c#程序执行cmd命令

  private void ExecuteCMD(string cmd)
        {
         
            Process process = new Process();

            try
            {
                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(cmd);
                process.StandardInput.WriteLine("exit");
                process.WaitForExit();

                string readProOutResult = process.StandardOutput.ReadToEnd();

            }
            catch (Exception ex)
            {
                MessageBox.Show("操作失败");
            }
            finally
            {
                process.Close();
            }

posted @ 2011-07-19 15:03  chenleinet  阅读(692)  评论(0编辑  收藏  举报