I am trying to change system date and time using the following C# code. But the result is no change is occuring in systemdate(no error is thrown either). I tried by uncommenting the commented portion of the code too. But no effect. :( Afterward I tried to change time from command prompt.(not using c#) Then it showed "A required privilage is not held by the client." I know this message may not have any connection with the code, but I am adding it to make my situation clearer.
[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);
}
Please help me to know what is going wrong. Thanks in advance. :)