C#,定時自動關機

來源:互聯網
上載者:User
一直想找一個可以自己實現定時自動關機的軟體,辛辛苦苦在網上找了一個,結果搞了幾次之後竟然要收費,閑來自己也寫一個試試。 這是一個很小的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();
     }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.