首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# 怎么实现关机重启

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

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楼学习
[解决办法]
七楼的方法还是相当简单的,
学习学习
[解决办法]
顶一个

热点排行