问题
I am new in C#, I am developing the project at C#.net windows application in that I need to read the next schedule time from my schedule file, if there is no schedule for next one hour, my system need not to wake on until next schedule time reach which is defined in schedule file. My system needs to shutdown, and before shutdown it need to set the timer for system wake. How to set the system on time before shutdown my pc.
I did this same in Linux system through python script by writing the system on time at /sys/class/rtc/rtc0/wakealaram
location. I just write the next system on time at this location and shutdown the system, the system can be automatically on by reach the time which is given in wakealaram
file. I need to do the same in Windows system by using C#.
回答1:
Have a look at below.
#region Clock Reset
[StructLayout(LayoutKind.Sequential)]
public struct SYSTEMTIME
{
public short Year;
public short Month;
public short DayOfWeek;
public short Day;
public short Hour;
public short Minute;
public short Second;
public short Milliseconds;
}
[DllImport("kernel32.dll")]
public extern static uint SetSystemTime(ref SYSTEMTIME lpSystemTime);
private void ResetSystemTime(DateTime tdt)
{
try
{
SYSTEMTIME time = new SYSTEMTIME();
time.Day = (short)tdt.Day;
time.Month = (short)tdt.Month;
time.Year = (short)tdt.Year;
time.Hour = (short)tdt.Hour;
time.Minute = (short)tdt.Minute;
time.Second = (short)tdt.Second;
SetSystemTime(ref time);
}
catch (Exception ex)
{
MessageBox.Show("System time reset Exception: " + ex.Message);
}
}
#endregion
来源:https://stackoverflow.com/questions/17379217/how-do-i-set-the-wake-up-time-to-system-before-shutdown-the-pc