一直想找一個可以自己實現定時自動關機的軟體,辛辛苦苦在網上找了一個,結果搞了幾次之後竟然要收費,閑來自己也寫一個試試。 這是一個很小的winform應用,使用VS2008+.NET 3.5環境開發,要在XP系統且安裝了.NET3.5的機器上才能使用,期待後續版本的升級。 本文著重介紹關機操作和定時的實現,其餘部分不再詳述。---------------------------------AutoShutDown V1.0-------------------------------自動關機部分的實現:Process proc = new Process();
proc.StartInfo.FileName = "cmd.exe";//start the command line
proc.StartInfo.UseShellExecute = false;//do not use shell mode instead by programme mode
proc.StartInfo.RedirectStandardError = true;//re-direct IO
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.CreateNoWindow = true;//do not creat a form
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();//BLANK is a const var with " "
string commandLine = String.Empty;
switch (option)
{
case Program.OptionType.isCancel:
commandLine += @"shutdown /a";
break;
case Program.OptionType.isLogoff:
commandLine += @"shutdown /f /l /t" + BLANK + interval;
break;
case Program.OptionType.isRestart:
commandLine += @"shutdown /f /r /t" + BLANK + interval;
break;
case Program.OptionType.isShutdown:
commandLine += @"shutdown /f /s /t" + BLANK + interval;
break;
}
proc.StandardInput.WriteLine(commandLine);建立一個Process對象,啟動命令列視窗,並使用不顯示視窗的模式,然後標記命令列命令進行操作。常用的命令列操作有:
-a or /a |
Abort/取消所有shutdown操作 |
-l or /l |
Log off |
-r or /r |
重新啟動 restart |
-s or /s |
Shutdown 關閉電腦 |
-t or /t xx |
在xx秒後進行操作 |
-----------------------------------------------winform定時器的實現:使用timer控制項的Tick事件來實現,事件代碼如下。private void timer_CountDown_Tick(object sender, EventArgs e)
{
if ((intervalSeconds - secondsFlush) == 0)
{
timer_CountDown.Stop();
toolStripStatusLabel_ShowText.Visible = false;
toolStripStatusLabel_TimeLeft.Visible = false;
ControlSpengding();
}
else
{
secondsFlush += 1;
toolStripStatusLabel_TimeLeft.Text = (intervalSeconds - secondsFlush).ToString();
}
}