Я пытаюсь изменить системную дату и время, используя следующий код С#. Но в результате в системной дате не происходит никаких изменений (ошибка также не возникает). Я также попытался раскомментировать закомментированную часть кода. Но никакого эффекта. :( После этого я попытался изменить время из командной строки (не используя С#). Затем он показал: «Клиент не имеет требуемой привилегии». Я знаю, что это сообщение может не иметь никакого отношения к коду, но я добавляю его чтобы прояснить мою ситуацию.
[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);
}
Пожалуйста, помогите мне узнать, что происходит не так. Заранее спасибо. :)
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
- person GauravKP   schedule 16.10.2016