Sto cercando di aggiornare il mio tempo di sistema utilizzando il seguente:Come aggiornare la data e/o l'ora del sistema utilizzando .NET
[StructLayout(LayoutKind.Sequential)]
private 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 wMilliseconds;
}
[DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
private extern static void Win32GetSystemTime(ref SYSTEMTIME lpSystemTime);
[DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
private extern static bool Win32SetSystemTime(ref SYSTEMTIME lpSystemTime);
public void SetTime()
{
TimeSystem correctTime = new TimeSystem();
DateTime sysTime = correctTime.GetSystemTime();
// Call the native GetSystemTime method
// with the defined structure.
SYSTEMTIME systime = new SYSTEMTIME();
Win32GetSystemTime(ref systime);
// Set the system clock ahead one hour.
systime.wYear = (ushort)sysTime.Year;
systime.wMonth = (ushort)sysTime.Month;
systime.wDayOfWeek = (ushort)sysTime.DayOfWeek;
systime.wDay = (ushort)sysTime.Day;
systime.wHour = (ushort)sysTime.Hour;
systime.wMinute = (ushort)sysTime.Minute;
systime.wSecond = (ushort)sysTime.Second;
systime.wMilliseconds = (ushort)sysTime.Millisecond;
Win32SetSystemTime(ref systime);
}
Quando metto a punto tutto sembra a posto e tutti i valori sono corretti, ma quando si chiama Win32SetSystemTime (ref systime) l'ora effettiva del sistema (tempo di visualizzazione) non cambia e rimane invariata. La parte strana è che quando chiamo Win32GetSystemTime (ref systime) mi dà il nuovo tempo aggiornato. Qualcuno può darmi un aiuto su questo?
come la vedo io, TimeSystem è non dalla classe sistema di base, sei sicuro che TimeSystem non cambiano il tempo? prova a commentare Win32SetSystemTime (ref systime); line – Avram