Setting the time programmatically in Windows 7

I am transferring an application from Windows 2000 (I do not ask) to Windows 7, and I need to replicate functionality that allows the user to set the time from the graphical interface. Previously, this was done when calling directly to "time" using the command line, but it seems that the user rights have changed slightly in Windows 7.

After doing some research, it seems that you can set the time using the kernel32.dll Win32SetSystemTime method Win32SetSystemTime , but you get the same permission problems. Reading MSDN. It seems to me that I need to enable SE_SYSTEMTIME_NAME , but no matter what I try, I can not do this work.

Does anyone have some sample code for Windows 7 to allow a call to the Win32SetSystemTime API?

+7
c # windows-7
source share
3 answers

Not sure why it is not working for you. The following code sets the time until today at 16:12 UTC. (Worked for me)

 public class Program { 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 = "SetSystemTime", SetLastError = true)] public extern static bool Win32SetSystemTime(ref SystemTime st); public static void Main(string[] args) { SystemTime st = new SystemTime { Year = 2010, Month = 10, Day = 18, Hour = 16, Minute = 12, DayOfWeek = 1 }; } } 

According to docs :

The calling process must have SE_SYSTEMTIME_NAME privilege. By default, this privilege is disabled. The SetSystemTime function allows the SE_SYSTEMTIME_NAME privilege before changing the system time and disables this privilege before returning. For more information, see Working with special privileges.

This does not seem to be a problem.

+7
source share

Well, if the worst comes to the worst, there is always

 System.Diagnostics.Process.Start("CMD", "/C TIME 19:58"); // set time to 7:58PM 
+6
source share

Your application should be upgraded to change the time (since changing the time may cause activity logs, etc. to be untrue), but not to change the time zone. Put the manifest in your application with requireAdministrator, and the application will raise. (To check this, before doing the manifest, right-click your exe and run it as Adminstrator. This will raise the application only once. Lifting up is another matter from starting up by someone who falls into the Administrators group. Choosing to use your authority.)

Most likely, the user will not like the UAC prompt, so if the time change is rare, split it into a separate exe, put the manifest in the main application using asInvoker, and the other to change the time with requireAdministrator, and start the time change from the main application using ShellExecute It’s ideal to have a button or menu item for this to happen and put a screen icon on it so that the UAC prompt does not surprise the user. I reject UAC tips that I did not expect.

+4
source share

All Articles