C#实现Cmd命令

来源:互联网 发布:swiper.js 内容滚动 编辑:程序博客网 时间:2024/05/18 22:53

 Process p = new Process();
            // 设定程序名 
            p.StartInfo.FileName = "cmd.exe";
            // 关闭Shell的使用 
            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(textBox1.Text);
            p.StandardInput.WriteLine("exit");


            //读取出输出结果 
            //while (!p.StandardOutput.EndOfStream)
            //{
            //    richTextBox1.AppendText(p.StandardOutput.ReadLine());
                //richTextBox1.AppendText(p.StandardOutput.ReadLine() + "<br />" +     Environment.NewLine);
            //}
            //p.WaitForExit();
            //p.Close();
            string strOutput = null;//输出结果
            strOutput = p.StandardOutput.ReadToEnd();
            MessageBox.Show(strOutput);

原创粉丝点击