日期:2014-05-18  浏览次数:20955 次

C# 如何实现关机重启
想做个定时关机重启的窗体程序 对关机重启的实现 网上找了下 说:使用C#调用cmd.exe执行shutdown.exe命令就行。

可行吗?可以的话 是怎么调用的? 还有其它简单的方法吗 麻烦进贴的各位了

------解决方案--------------------
我给你这两个方法copy走吧,拿去粘贴在你的项目里就能用~~~记得调用这两个方法哦~~~

C# code

       //------------关机方法
        public void guanji() 
        {
            try
            {
                //启动本地程序并执行命令
                Process.Start("Shutdown.exe", " -s -t 0");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
       //----------重启重启
       public void chongqi()
       {
           try
           {
               //启动本地程序并执行命令
               Process.Start("shutdown.exe"," -r -t 0");
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }

//赠送一个使用win32API函数注销的

        //===================================================================================注销 函数 声明
        [DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
        //ExitWindowsEx 函数
        private static extern int ExitWindowsEx(int uFlags, int dwReserved);
        //======================================================================================
        public void zhuxiao() //注销
        {
            ExitWindowsEx(0, 0);
        }

------解决方案--------------------
C# code
                Process p = new Process();                

                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.Arguments = "/c shutdown.exe -r -t 0";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;

                p.Start();
                p.StandardInput.WriteLine("exit");

------解决方案--------------------
操作系统提供了关机控制的api函数
------解决方案--------------------
up
------解决方案--------------------
用API好些,用DOS命令如果系统没那命令就不可以了,
------解决方案--------------------
C# code

   private void button1_Click(object sender, EventArgs e)//关机
        {
            Process process = new Process();
            process.StartInfo.FileName = "shutdown.exe";
            process.StartInfo.Arguments = "-s -t 0";
            process.Start(); 

        }

        private void button2_Click(object sender, EventArgs e)//重启
        {
            Process process = new Process();
            process.StartInfo.FileName = "shutdown.exe";
            process.StartInfo.Arguments = "-r -t 0";
            process.Start();
        }

------解决方案--------------------
向7楼学习
------解决方案--------------------
七楼的方法还是相当简单的,
学习学习
------解决方案--------------------
顶一个