传递进来的参数是合法的日期时间,但就是设置不成功。[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;
    }
    public class Win32
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern int SetSystemTime(ref SystemTime lpSystemTime);
        public static bool SetSystemTime(string strDateTime)
        {
            if (strDateTime.Length != 12)
            {
                return false;            }
            SystemTime sysTime = new SystemTime();
            sysTime.wYear = Convert.ToUInt16(strDateTime.Substring(0, 2));
            sysTime.wMonth = Convert.ToUInt16(strDateTime.Substring(2, 2));
            sysTime.wDay = Convert.ToUInt16(strDateTime.Substring(4, 2));
            sysTime.wHour = Convert.ToUInt16(strDateTime.Substring(6, 2));
            sysTime.wMinute = Convert.ToUInt16(strDateTime.Substring(8, 2));
            sysTime.wSecond = Convert.ToUInt16(strDateTime.Substring(10, 2));
            int flag = Win32.SetSystemTime(ref sysTime);
            return Convert.ToBoolean(flag);
        }
    }

解决方案 »

  1.   

    lz:是你的这部分代码有问题:            sysTime.wYear = Convert.ToUInt16(strDateTime.Substring(0, 2));
                sysTime.wMonth = Convert.ToUInt16(strDateTime.Substring(2, 2));
                sysTime.wDay = Convert.ToUInt16(strDateTime.Substring(4, 2));
                sysTime.wHour = Convert.ToUInt16(strDateTime.Substring(6, 2));
                sysTime.wMinute = Convert.ToUInt16(strDateTime.Substring(8, 2));
                sysTime.wSecond = Convert.ToUInt16(strDateTime.Substring(10, 2));我做了个实验:[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;
        }
        public class MyClass
        {
            [DllImport("kernel32.dll", SetLastError = true)]
            static extern int GetSystemTime(ref SystemTime lpSystemTime);        [DllImport("kernel32.dll", SetLastError = true)]
            static extern int SetSystemTime(ref SystemTime lpSystemTime);        public static int GetTime(ref SystemTime lpSystemTime)
            {
                return GetSystemTime(ref lpSystemTime);
            }        public static bool SetTime(ref SystemTime lpSystemTime)
            {
                int flag = SetSystemTime(ref lpSystemTime);
                return Convert.ToBoolean(flag);
            }
        }
            private void button1_Click(object sender, EventArgs e)
            {
                SystemTime time = new SystemTime();
                MyClass.GetTime(ref time);            bool b = MyClass.SetTime(ref time);
            }程序完全正确!你仔细检查那部分代码,年月日时分秒都有限制的!!