如何在程序中得到"DOS"或"CMD.exe"中的执行结果 Process cmd = new Process();string result = cmd.StandardOutput.ReadToEnd(); 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Process cmd = new Process();cmd.StartInfo.FileName = "";cmd.StartInfo.UseShellExecute = false; cmd.StartInfo.RedirectStandardInput = true;cmd.StartInfo.RedirectStandardOutput = true; cmd.StartInfo.CreateNoWindow = true;cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;cmd.Start(); this.textBox1.Text = cmd.StandardOutput.ReadToEnd();cmd.WaitForExit();cmd.Close(); Process cmd = new Process();cmd.Start();string result = cmd.StandardOutput.ReadToEnd(); 引用错了 ,是这个方法 我试了 每次在readtoend的时候就已经结束了 没反应了 呵呵,这个可能更好点吧。 //用于执行dos命令的方法 private string Execute(string dosCommand) { Thread.Sleep(150); string output = ""; Process process = new Process(); try { process.StartInfo.WorkingDirectory = @"C:\WINDOWS\system32"; process.StartInfo.FileName = "cmd.exe"; process.StartInfo.Arguments = "/c " + dosCommand; process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardInput = true; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; process.StartInfo.CreateNoWindow = true; process.Start(); process.WaitForExit(1); output = process.StandardError.ReadToEnd(); } catch (Exception ex) { output = ex.Message; } if (process != null) process.Close(); return output; } output = process.StandardOutput.ReadToEnd();output += process.StandardError.ReadToEnd(); 没试过,可能会报错。 还不是行,也在readtoend那个地方就不动了 在ReadToEnd()之前启动cmdProcess cmd = new Process();cmd.Start();string result = cmd.StandardOutput.ReadToEnd();cmd.WaitForExit(); 我是照猫画虎,我也不知道参数设置的对不对. 已经start了 但是... Process cmdProc = new Process(); cmdProc.StartInfo.FileName = "your exe file name"; cmdProc.StartInfo.UseShellExecute = false; cmdProc.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory; cmdProc.StartInfo.Arguments = "your argument type here"; cmdProc.StartInfo.CreateNoWindow = true; cmdProc.StartInfo.RedirectStandardOutput = true; cmdProc.OutputDataReceived += (s, e) => { Console.WriteLine(e.Data); }; cmdProc.Start(); cmdProc.BeginOutputReadLine();关键的几句: //输出重定向 cmdProc.StartInfo.RedirectStandardOutput = true; //注册输出变化事件 cmdProc.OutputDataReceived += (s, e) => { Console.WriteLine(e.Data); }; //开启进程 cmdProc.Start(); //开启之后,开始读取输出 //如果有输出,你设置断点可看到Console.WriteLine(e.Data);不停被调用. cmdProc.BeginOutputReadLine(); s和e是什么东东?object EventArgs?还有 你这是用Linq写的吗? 看不到 任何输出调试的时候他给了一个 pro.ExitCode异常和pro.ExitTime异常. faint lz把你代码贴出来看看。 我也对此感兴趣呵呵 check http://blog.csdn.net/sxldfang/archive/2010/10/22/5959873.aspx string str = ""; lblmanager.Text = ""; Process pro = new Process(); pro.StartInfo.FileName = "cmd.exe"; pro.StartInfo.UseShellExecute = false; pro.StartInfo.RedirectStandardInput = true; pro.StartInfo.RedirectStandardOutput = true; pro.StartInfo.RedirectStandardError = true; pro.StartInfo.CreateNoWindow = true; pro.Start(); if (textBox1.Text == "") { MessageBox.Show("请输入指令"); return; } else { pro.StandardInput.Write("@echo off"); pro.StandardInput.Write(textBox1.Text); pro.StandardInput.Write("Exit"); Console.WriteLine( pro.StandardOutput.ReadToEnd()); pro.WaitForExit(1); pro.Close(); }这是我的源码 大家帮我看下 有什么错!!!!! 我空间里有个经典的Delphi版的 菜鸟请教有关博客开发的经典问题! post里的&怎么传 关于屏幕键盘 急!! installshield 8+.net制作安装文件后安装的问题(.net1.1) 什么是堆栈? 用VSS进行过WEB开发的兄弟进来帮下我这个才入门的菜鸟啊... 请问在C#中能否调用“Microsoft.VisualBasic”命名空间下的函数? 我在用c#做oa系统!想结交这方面的朋友!有兴趣的朋友请进!! 求注释程序!!!最好一段一注释!!!,不行一行也行!!!! 怎么把主线程跟附加线程分离? 网站后面是aspx是什么意思 C#串口编程书
cmd.StartInfo.FileName = "";
cmd.StartInfo.UseShellExecute = false;
cmd.StartInfo.RedirectStandardInput = true;
cmd.StartInfo.RedirectStandardOutput = true;
cmd.StartInfo.CreateNoWindow = true;
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
cmd.Start();
this.textBox1.Text = cmd.StandardOutput.ReadToEnd();
cmd.WaitForExit();
cmd.Close();
cmd.Start();
string result = cmd.StandardOutput.ReadToEnd();
//用于执行dos命令的方法
private string Execute(string dosCommand)
{
Thread.Sleep(150);
string output = "";
Process process = new Process();
try
{
process.StartInfo.WorkingDirectory = @"C:\WINDOWS\system32";
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/c " + dosCommand;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.WaitForExit(1);
output = process.StandardError.ReadToEnd();
}
catch (Exception ex)
{
output = ex.Message;
}
if (process != null)
process.Close();
return output;
}
output = process.StandardOutput.ReadToEnd();
output += process.StandardError.ReadToEnd(); 没试过,可能会报错。
还不是行,也在readtoend那个地方就不动了
在ReadToEnd()之前启动cmdProcess cmd = new Process();
cmd.Start();
string result = cmd.StandardOutput.ReadToEnd();
cmd.WaitForExit();
cmdProc.StartInfo.FileName = "your exe file name";
cmdProc.StartInfo.UseShellExecute = false;
cmdProc.StartInfo.WorkingDirectory = AppDomain.CurrentDomain.BaseDirectory;
cmdProc.StartInfo.Arguments = "your argument type here";
cmdProc.StartInfo.CreateNoWindow = true;
cmdProc.StartInfo.RedirectStandardOutput = true;
cmdProc.OutputDataReceived += (s, e) =>
{
Console.WriteLine(e.Data);
};
cmdProc.Start();
cmdProc.BeginOutputReadLine();关键的几句: //输出重定向
cmdProc.StartInfo.RedirectStandardOutput = true;
//注册输出变化事件
cmdProc.OutputDataReceived += (s, e) =>
{
Console.WriteLine(e.Data);
};
//开启进程
cmdProc.Start();
//开启之后,开始读取输出
//如果有输出,你设置断点可看到Console.WriteLine(e.Data);不停被调用.
cmdProc.BeginOutputReadLine();
s和e是什么东东?object EventArgs?
还有 你这是用Linq写的吗?
调试的时候他给了一个
pro.ExitCode异常和pro.ExitTime异常.
lblmanager.Text = "";
Process pro = new Process();
pro.StartInfo.FileName = "cmd.exe";
pro.StartInfo.UseShellExecute = false;
pro.StartInfo.RedirectStandardInput = true;
pro.StartInfo.RedirectStandardOutput = true;
pro.StartInfo.RedirectStandardError = true;
pro.StartInfo.CreateNoWindow = true;
pro.Start(); if (textBox1.Text == "")
{
MessageBox.Show("请输入指令");
return;
}
else
{
pro.StandardInput.Write("@echo off");
pro.StandardInput.Write(textBox1.Text);
pro.StandardInput.Write("Exit");
Console.WriteLine( pro.StandardOutput.ReadToEnd());
pro.WaitForExit(1);
pro.Close();
}
这是我的源码 大家帮我看下 有什么错!!!!!