如何在c#[C/S程序]中获取cmd命令的返回信息
Process por = new Process();
por.StartInfo.FileName = "cmd.exe";
por.StartInfo.UseShellExecute = false;
por.StartInfo.RedirectStandardInput = true;
por.StartInfo.RedirectStandardOutput = true;
por.StartInfo.RedirectStandardError = true;
por.StartInfo.CreateNoWindow = true;
string strOutput = null;//接收DOS命令执行结果
por.Start();
por.StandardInput.WriteLine(TextBox1.Text);
por.StandardInput.WriteLine("Exit");
//textBox2.Text = por.StandardOutput.ReadToEnd();
StreamReader sr = por.StandardOutput;
string line="";
string line2="";
while ((line=sr.ReadLine()) != null)
{
line2+=line+"\n";
}
TextBox2.Text = line2;
以上是我的代码----是我在B/S,asp。net中代码【可以根据输入的CMD命令,将返回结果显示出来】,但代码如果在C/S中,执行结果是弹出一个一模一样的窗体来,不知是什么原因,有人搞过这方面的,请帮忙写下代码【我要C/S代码】,谢谢
--------------------编程问答-------------------- 帮顶下。 --------------------编程问答-------------------- Process por = new Process();
por.StartInfo.FileName = "cmd.exe";
por.StartInfo.Arguments = "/c "+TextBox1.Text;
por.StartInfo.UseShellExecute = false;
por.StartInfo.RedirectStandardInput = true;
por.StartInfo.RedirectStandardOutput = true;
por.StartInfo.RedirectStandardError = true;
por.StartInfo.CreateNoWindow = true;
por.Start();
StreamReader sr = por.StandardOutput;
string line = "";
string line2 = "";
while ((line = sr.ReadLine()) != null)
{
line2 += line + "\n";
}
TextBox2.Text = line2;
--------------------编程问答-------------------- 楼主 什么意思? --------------------编程问答-------------------- 在C/S中,楼主给出的代码,一运行 就弹出一个一模一样的窗体,
无语。 --------------------编程问答-------------------- 请贴代码的朋友,注意,在B/S是可以的,在C/S里不行,
不要再贴相同的代码了。 --------------------编程问答-------------------- 帮你顶上去
--------------------编程问答-------------------- 那就只有帮顶一下了 --------------------编程问答-------------------- .................... --------------------编程问答-------------------- 帮顶下。
--------------------编程问答-------------------- 没弄过,顶下吧 --------------------编程问答-------------------- Process process = new Process();
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("sftp2 -b");
// SendKeys.Send("{enter}");
//process.StandardInput.WriteLine("quit");
process.StandardInput.WriteLine("exit");
richTextBox1.AppendText(process.StandardOutput.ReadToEnd());
process.Close(); --------------------编程问答-------------------- 这是个简单的例子,在button等空间的响应事件中调用这个方法就行了。
--------------------编程问答-------------------- 有点不懂。。。。。。 --------------------编程问答-------------------- 发一个ping的代码你参考下:
public void cmd()
{
Process process = new Process();
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("ping www.baidu.com");
process.StandardInput.WriteLine("exit");
String a = "";
textBox1.Text = "";
int b = 1;
while (b == 1)
{
a = process.StandardOutput.ReadLine();
textBox1.Text = textBox1.Text + a + "\r\n";
if (process.StandardOutput.EndOfStream)
{
b = 0;
}
}
/*textBox1.AppendText(process.StandardOutput.ReadToEnd());*/
process.Close();
}
tbResult.Text = "";
ProcessStartInfo start = new ProcessStartInfo("Ping.exe");//设置运行的命令行文件问ping.exe文件,这个文件系统会自己找到
//如果是其它exe文件,则有可能需要指定详细路径,如运行winRar.exe
start.Arguments = txtCommand.Text;//设置命令参数
start.CreateNoWindow = true;//不显示dos命令行窗口
start.RedirectStandardOutput = true;//
start.RedirectStandardInput = true;//
start.UseShellExecute = false;//是否指定操作系统外壳进程启动程序
Process p = Process.Start(start);
StreamReader reader = p.StandardOutput;//截取输出流
string line = reader.ReadLine();//每次读取一行
while (!reader.EndOfStream)
{
tbResult.AppendText(line + " ");
line = reader.ReadLine();
}
p.WaitForExit();//等待程序执行完退出进程
p.Close();//关闭进程
reader.Close();//关闭流 --------------------编程问答-------------------- 关注
帮定 --------------------编程问答-------------------- mark study up --------------------编程问答-------------------- 把 进程设置到后台执行就行了 --------------------编程问答-------------------- por.StartInfo.RedirectStandardOutput = true;
por.StartInfo.RedirectStandardError = true;
StreamReader sr1 = por.StandardOutput;//正常输入内容
StreamReader sr2 = por.StandardError;//错误输出
string line = sr1.readtoend();
string line2 =sr2.readtoend(); --------------------编程问答-------------------- 看看添加
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
有用没 --------------------编程问答-------------------- object..CreateNoWindow = true;//不显示dos命令行窗口
这个加上不行吗?
补充:.NET技术 , 其他语言