How can I change the local system's date & time programmatically with C#?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Both require that the caller has been granted SeSystemTimePrivilege and that this privilege is enabled.
Since I mentioned it in a comment, here's a C++/CLI wrapper:
The C# client code is now very simple:
Here is where I found the answer.
I have reposted it here to improve clarity.
Define this structure:
Add the following
extern
method to your class:Then call the method with an instance of your struct like this:
Use this function to change the time of system (tested in window 8)
Example: Call in load method of form setDate("5-6-92"); setTime("2:4:5 AM");
This Is Work Function:
A lot of great viewpoints and approaches are already here, but here are some specifications that are currently left out and that I feel might trip up and confuse some people.
SetSystemTime
function. The reason is that calling process needs the SE_SYSTEMTIME_NAME privilege.SetSystemTime
function is expecting aSYSTEMTIME
struct in coordinated universal time (UTC). It will not work as desired otherwise.Depending on where/ how you are getting your
DateTime
values, it might be best to play it safe and useToUniversalTime()
before setting the corresponding values in theSYSTEMTIME
struct.Code example: