希望自己能在 IT者条道路上越走越远!

C# 执行CMD 命令 远程关机

 void shutdown()        {            Process commandProcess = new Process();       
    
 try            {               
     commandProcess.StartInfo.FileName = "cmd.exe";            
     commandProcess.StartInfo.UseShellExecute = false;           
     commandProcess.StartInfo.CreateNoWindow = true;         
     commandProcess.StartInfo.RedirectStandardError = true;        
     commandProcess.StartInfo.RedirectStandardInput = true;         
     commandProcess.StartInfo.RedirectStandardOutput = true;        
     commandProcess.Start();        
     commandProcess.StandardInput.WriteLine("shutdown /r /m 127.0.0.1 /t 200 /f");      
     commandProcess.StandardInput.WriteLine("exit");     
 
           for ( !commandProcess.HasExited; )//等待cmd命令运行完毕                {     
               System.Threading.Thread.Sleep(1);                }                //错误输出     
          string tmpout = commandProcess.StandardError.ReadToEnd();      
          string tmpout1 = commandProcess.StandardOutput.ReadToEnd();      
 
      }      
        catch (Exception e)            {           
         MessageBox.Show(e.Message);         
   }         
         finally            {          
      if (commandProcess != null)  {         
           commandProcess.Dispose();        
            commandProcess = null;        
        }       
     }   
 
     }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
posted @ 2016-01-17 01:03  时光犹存少年梦  阅读(702)  评论(0编辑  收藏  举报