【问题标题】:Change system time programmaticaly using datetimepeaker使用 datetimepicker 以编程方式更改系统时间
【发布时间】:2016-10-15 17:54:55
【问题描述】:

我正在尝试使用以下 C# 代码更改系统日期和时间。但结果是 systemdate 中没有发生任何变化(也没有引发错误)。我也尝试取消注释代码的注释部分。但是没有效果。 :( 之后我尝试从命令提示符更改时间。(不使用 c#)然后它显示“客户端未持有所需的特权”。我知道这条消息可能与代码没有任何联系,但我添加它是为了让我的情况更清楚。

   [StructLayout(LayoutKind.Sequential)]
      public struct Systemtime
     {
        public ushort Year;
        public ushort Month;
        public ushort DayOfWeek;
        public ushort Day;
        public ushort Hour;
        public ushort Minute;
        public ushort Second;
        public ushort Millisecond;
     }
    /*
            [DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
            [return: MarshalAsAttribute(UnmanagedType.Bool)]
            public static extern bool Win32GetSystemTime([InAttribute]ref Systemtime sysTime);
     */
    [DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
    [return: MarshalAsAttribute(UnmanagedType.Bool)]
    public static extern bool Win32SetSystemTime([InAttribute]ref Systemtime sysTime);

    private void DateTimeSelectedButton_Click(object sender, EventArgs e)
    {
        //var neededtime = MonthChangePhenomenonDateTimePicker.Value - TimeSpan.FromHours(5)-TimeSpan.FromMinutes(30);
        // Set system date and time

        Systemtime updatedTime = new Systemtime();
        updatedTime.Year = (ushort)MonthChangePhenomenonDateTimePicker.Value.Year;
        updatedTime.Month = (ushort)MonthChangePhenomenonDateTimePicker.Value.Month;
        updatedTime.Day = (ushort)MonthChangePhenomenonDateTimePicker.Value.Day;

        // UTC time; it will be modified according to the regional settings of the target computer so the actual hour might differ
        updatedTime.Hour = (ushort)MonthChangePhenomenonDateTimePicker.Value.Hour;
        updatedTime.Minute = (ushort)MonthChangePhenomenonDateTimePicker.Value.Minute;
        updatedTime.Second = (ushort)MonthChangePhenomenonDateTimePicker.Value.Second;
        // Call the unmanaged function that sets the new date and time instantly
        Win32SetSystemTime(ref updatedTime);

        MessageBox.Show(@"You Have Current System Time: " + updatedTime);
    }

请帮助我知道出了什么问题。提前致谢。 :)

【问题讨论】:

  • 把Win32SetSystemTime改成SetSystemTime再试试。
  • 刚试过@vivek nuna 但没有效果:(
  • 以管理员身份运行您的代码,然后尝试
  • 怎么做!? @vivek 努纳
  • 写点击visual studio,你会看到以管理员身份运行

标签: c# time systemtime


【解决方案1】:

此后是一个 C# 控制台应用程序,用于根据 Tri Nguyen 博客的帖子“C# – How to enable SeDebugPrivilege?”提高 SE_SYSTEMTIME_NAME,并在我的 Windows 7 64 位笔记本电脑上进行了测试。 请不要忘记在此博客上发表评论。

using System;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr GetCurrentProcess();

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern bool CloseHandle(IntPtr hHandle);

        [StructLayout(LayoutKind.Sequential)]
        public struct LUID
        {
            public UInt32 LowPart;
            public Int32 HighPart;
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct TOKEN_PRIVILEGES
        {
            public UInt32 PrivilegeCount;
            public LUID Luid;
            public UInt32 Attributes;
        }

        [DllImport("advapi32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool OpenProcessToken(IntPtr ProcessHandle,
            UInt32 DesiredAccess, out IntPtr TokenHandle);

        [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool LookupPrivilegeValue(string lpSystemName, string lpName,
            out LUID lpLuid);

        // Use this signature if you do not want the previous state
        [DllImport("advapi32.dll", SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool AdjustTokenPrivileges(IntPtr TokenHandle,
           [MarshalAs(UnmanagedType.Bool)]bool DisableAllPrivileges,
           ref TOKEN_PRIVILEGES NewState,
           UInt32 Zero,
           IntPtr Null1,
           IntPtr Null2);

        private static uint TOKEN_QUERY = 0x0008;
        private static uint TOKEN_ADJUST_PRIVILEGES = 0x0020;

        public const UInt32 SE_PRIVILEGE_ENABLED = 0x00000002;

        public const string SE_SYSTEMTIME_NAME = "SeSystemtimePrivilege";

        static void Main(string[] args)
        {
            IntPtr hToken;
            string sSEPrivilegeName = SE_SYSTEMTIME_NAME;
            LUID luidSEPrivilegeNameValue;
            TOKEN_PRIVILEGES tkpPrivileges;

            if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out hToken))
            {
                Console.WriteLine("OpenProcessToken() failed, error = {0}. {1} is not available", Marshal.GetLastWin32Error(), sSEPrivilegeName);
                return;
            }
            else
            {
                Console.WriteLine("OpenProcessToken() successfully");
            }

            if (!LookupPrivilegeValue(null, SE_SYSTEMTIME_NAME, out luidSEPrivilegeNameValue))
            {
                Console.WriteLine("LookupPrivilegeValue() failed, error = {0}. {1} is not available", Marshal.GetLastWin32Error(), sSEPrivilegeName);
                CloseHandle(hToken);
                return;
            }
            else
            {
                Console.WriteLine("LookupPrivilegeValue() successfully");
            }

            tkpPrivileges.PrivilegeCount = 1;
            tkpPrivileges.Luid = luidSEPrivilegeNameValue;
            tkpPrivileges.Attributes = SE_PRIVILEGE_ENABLED;

            if (!AdjustTokenPrivileges(hToken, false, ref tkpPrivileges, 0, IntPtr.Zero, IntPtr.Zero))
            {
                Console.WriteLine("LookupPrivilegeValue() failed, error = {0}. {1} is not available", Marshal.GetLastWin32Error(), sSEPrivilegeName);
            }
            else
            {
                Console.WriteLine("{0} is now available", sSEPrivilegeName);
            }
            CloseHandle(hToken);
            Console.ReadLine();
        }
    }
}

【讨论】:

    【解决方案2】:

    在调用“SetSystemTime”函数之前,您必须使用“AdjustTokenPrivileges”函数提升 SE_SYSTEMTIME_NAME 权限。 该函数位于 Advapi32.dll 中。

    【讨论】:

    • 您好 J. Piquard,感谢您的回答。但是,我不知道如何使用它。我看到了一些文档,但没有一个真正有帮助。 :(你能提供一些简单的代码吗。对于c#。对于这个问题,我打算通过使用清单文件来完成它。再次感谢你。
    猜你喜欢
    • 2011-07-30
    • 2013-08-21
    • 2010-10-13
    • 2016-04-20
    • 1970-01-01
    • 2018-05-06
    • 1970-01-01
    • 2013-09-27
    相关资源
    最近更新 更多