当前位置:编程学习 > C#/ASP.NET >>

请教各位大侠 c#怎样修改系统时间??急!!

应用Datetime能不能修时间
例如
       
    string time = Encoding.ASCII.GetString(“2011-09-27   08:09:10”);
怎样才能把系统时间改为2011年9月27日 8点9分10秒??
--------------------编程问答-------------------- 你的意思是用C#修改 PC时间吗? --------------------编程问答-------------------- http://hi.baidu.com/ysdonet/blog/item/88fe5e0250ecbd0a4bfb5133.html

Win7、Vista保证程序要有管理员权限。 --------------------编程问答--------------------

可以用API来做: 
[StructLayout (LayoutKind.Sequential)] 
public struct SYSTEMTIME 

public short Year; 
public short Month; 
public short DayOfWeek; 
public short Day; 
public short Hour; 
public short Minute; 
public short Second; 
public short Miliseconds; 



//api函数声明 
[DllImport ("kernel32.dll", CharSet=CharSet.Ansi)] 
public extern static bool SetSystemTime(ref SYSTEMTIME time); 

private void button1_Click(object sender, System.EventArgs e) 

//调用代码 
SYSTEMTIME t = new SYSTEMTIME (); 
t.Year = 2000; 
t.Month = 1; 
t.Day = 2; 
t.Hour = 12-8; //这个函数使用的是0时区的时间,对于我们用+8时区的,时间要自己算一下.如要设12点,则为12-8 
t.Minute = 5; 
bool v = SetSystemTime(ref t); 
Console.WriteLine(v.ToString()); 
}

--------------------编程问答-------------------- 新建一个类文件SystemDateTime.cs:
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace setDateTime
{
    class SystemDateTime
    {
        [DllImport("Kernel32.dll")]
        public static extern bool SetLocalTime(ref SystemTime sysTime);

        [DllImport("Kernel32.dll")]
        public static extern void GetLocalTime(ref SystemTime sysTime);
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct SystemTime
    {
        public ushort wYear;
        public ushort wMonth;
        public ushort wDayOfWeek;
        public ushort wDay;
        public ushort wHour;
        public ushort wMinute;
        public ushort wSecond;
        public ushort wMiliseconds;
    }
}




 然后是你的文件:
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace setDateTime
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now.AddDays(-5);
            this.Text = Form1.SetSysTime(dt).ToString();

        }

        public static bool SetSysTime(DateTime newdatetime)
        {
            SystemTime st = new SystemTime();
            st.wYear = (ushort)(newdatetime.Year - 1);
            st.wMonth = (ushort)newdatetime.Month;
            st.wDayOfWeek = (ushort)3;
            st.wDay = (ushort)newdatetime.Day;
            st.wHour = (ushort)(newdatetime.Hour - 2);
            st.wMinute = (ushort)newdatetime.Minute;
            st.wSecond = (ushort)newdatetime.Second;
            st.wMiliseconds = (ushort)newdatetime.Millisecond;
            return SystemDateTime.SetLocalTime(ref st);
        }
    }
} --------------------编程问答-------------------- 调用DataTime.ToUniversalTime将本地时间转换成UTC时间,之后调用WinAPI  SetSystemTime --------------------编程问答-------------------- 谢谢,各位大侠!
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,