C # Use the WINDOWAPI function to modify the computer's date and time and format, the code snippet is as follows, have a friend to take it.
The code is as follows |
Copy Code |
Public Class Setcomputerdatetime { [DllImport ("kernel32.dll", EntryPoint = "GetSystemDefaultLCID")] public static extern int getsystemdefaultlcid (); [DllImport ("kernel32.dll", EntryPoint = "Setlocaleinfoa")] public static extern int SetLocaleInfo (int Locale, int lctype, string lplcdata); public const int locale_slongdate = 0x20; public const int locale_sshortdate = 0x1F; public const int locale_stime = 0x1E; [DllImportAttribute ("Kernel32.dll")] public static extern void Setlocaltime (SystemTime st); public ushort Wyear; public ushort Wmonth; public ushort Wdayofweek; public ushort Wday; public ushort Whour; public ushort Wminute; public ushort Wsecond; public ushort Wmilliseconds; Public Setcomputerdatetimeformat () { } public void Setdatetimeformat () { Try { int x = GetSystemDefaultLCID (); SetLocaleInfo (x, Locale_sshortdate, "yyyy/mm/dd"); Short Date format SetLocaleInfo (x, Locale_slongdate, "yyyy year mm month DD Day"); Long Date format SetLocaleInfo (x, Locale_stime, "HH:mm:ss tt"); Time format } catch (Exception ex) { Console.WriteLine (ex); } } public void SetDateTime (datetime datetime) { Try { SystemTime mysystemtime = new SystemTime (); MySystemTime. Wyear = (ushort) systemdatetime.year;//year MySystemTime. Wmonth = (ushort) systemdatetime.month;//Month MySystemTime. Wday = (ushort) systemdatetime.day;//Day MySystemTime. Whour = (ushort) systemdatetime.hour;// MySystemTime. Wminute = (ushort) systemdatetime.minute;//min. MySystemTime. Wsecond = (ushort) systemdatetime.second;//sec Setlocaltime (MySystemTime); } catch (Exception ex) { Console.WriteLine (ex); } } } [StructLayoutAttribute (LayoutKind.Sequential)] public class SystemTime { public ushort Wyear; public ushort Wmonth; public ushort Wdayofweek; public ushort Wday; public ushort Whour; public ushort Wminute; public ushort Wsecond; public ushort Wmilliseconds; } |