C#執行cmd命令實現電腦關機

來源:互聯網
上載者:User

標籤:

C#實現執行CMD命令,實現電腦立即/定時關機
該篇博文主要介紹個人寫的一款實現立即與定時關機的軟體:
主要是通過調用window下的cmd.exe,然後執行關機相關的cmd命令,實現電腦的立即和定時關機,具體實現如下。首先是開啟系統內建的cmd.exe:
 <span style="white-space:pre"></span>    Process process = new Process();            process.StartInfo.FileName = "cmd.exe";            process.StartInfo.UseShellExecute = false;            process.StartInfo.RedirectStandardInput = true;            process.StartInfo.RedirectStandardOutput = true;            process.StartInfo.RedirectStandardError = true;            process.StartInfo.CreateNoWindow = true;            process.Start();

.Start以後便可輸入cmd命令執行:
            process.StandardInput.WriteLine("shutdown -s -t 3");            process.StandardInput.WriteLine("exit");            Shuttime = System.Int32.Parse(Intime.Text);            process.WaitForExit();            process.Close();

下面是定時自動關機的實現,跟上面實現方式相同:
 <span style="white-space:pre"></span>    string time;            time = Intime.Text;            Process process = new Process();            process.StartInfo.FileName = "cmd.exe";            process.StartInfo.UseShellExecute = false;            process.StartInfo.RedirectStandardInput = true;            process.StartInfo.RedirectStandardOutput = true;            process.StartInfo.RedirectStandardError = true;            process.StartInfo.CreateNoWindow = true;            process.Start();            process.StandardInput.WriteLine("shutdown -s -t " + time);            process.StandardInput.WriteLine("exit");            Shuttime = System.Int32.Parse(Intime.Text);            process.WaitForExit();            process.Close();

其中time是擷取到的輸入的秒數。
源碼資源:http://download.csdn.net/detail/laozhuxinlu/9512547

C#執行cmd命令實現電腦關機

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.