C#中使用DOS命令關閉當前正在啟動並執行程式並重新啟動

來源:互聯網
上載者:User

         在C#中可以利用通過Pocess.Start調用Dos命令來完成很多操作,而且非常方便。例如關閉當前的應用程式,重新啟動電腦等。

        下面介紹幾個常用的命令:

//重新啟動電腦的命令

private void RestartPC()

{

        //重啟電腦的命令發出
        Process.Start("shutdown.exe", "-r");
        //關閉所有相關進程
        Process.GetCurrentProcess().Kill();

}

//關閉電腦的命令

private void ShutDownPC()

{

        //重啟電腦的命令發出
        Process.Start("shutdown.exe");
        //關閉所有相關進程
        Process.GetCurrentProcess().Kill();

}

//重載關閉電腦函數,可以設定倒計時

        public static void ShutDownPC(string time)
        {
            string s = "-s -t " + time;
            Process.Start("shutdown.exe", s);

        }

        //登出登入

        public static void LogOff()
        {
            Process.Start("shutdown.exe", "-l");
        }

         //撤銷關閉電腦

         public static void CancelShutDown()
        {
            Process.Start("shutdown.exe", "-a");
        }

//開啟某個應用程式,例如開啟升級程式

    /// <summary>
    /// 啟動升級程式
    /// </summary>
    private void StartUpdateSys()
        {
        //啟動升級程式
        Process[] VAproc = Process.GetProcessesByName("VersionAgent");
        if (VAproc.Length == 0)
            {
            Process proc = new Process();
            proc.StartInfo.FileName = "VersionAgent.exe";
            proc.StartInfo.Arguments = "/p1     /b38400     /fstock     /mr ";
            proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            proc.Start();
            }
        }

            /// <summary>            /// 設定程式開機運行            /// </summary>            /// <param name="started">是否開機運行</param>            /// <param name="exeName">要啟動並執行EXE程式名稱(不要拓展名)</param>            /// <param name="path">要啟動並執行EXE程式路徑</param>            /// <returns>成功返回真,否則返回假</returns>            public bool RunWhenStart(bool started, string exeName, string path)            {                RegistryKey key = Registry.LocalMachine.OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);//開啟註冊表子項                if (key == null)//如果該項不存在的話,則建立該子項                {                    key = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");                }                if (started == true)                {                    try                    {                        key.SetValue(exeName, path);//設定為開機啟動                        key.Close();                    }                    catch                    {                        return false;                    }                }                else                {                    try                    {                        key.DeleteValue(exeName);//取消開機啟動                        key.Close();                    }                    catch                    {                        return false;                    }                }                return true;            }

相關文章

聯繫我們

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