如何在c#[C/S程序]中获取cmd命令的返回信息

zhengyongxianglove 2009-05-03 10:04:47

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代码】,谢谢

...全文
897 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
weiyiVB 2012-04-27
  • 打赏
  • 举报
回复
object..CreateNoWindow = true;//不显示dos命令行窗口
这个加上不行吗?
yikunmaodun 2010-10-12
  • 打赏
  • 举报
回复
看看添加
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
有用没
linwanhai 2010-09-20
  • 打赏
  • 举报
回复
por.StartInfo.RedirectStandardOutput = true;
por.StartInfo.RedirectStandardError = true;
StreamReader sr1 = por.StandardOutput;//正常输入内容
StreamReader sr2 = por.StandardError;//错误输出
string line = sr1.readtoend();
string line2 =sr2.readtoend();
pwg21 2010-09-08
  • 打赏
  • 举报
回复
把 进程设置到后台执行就行了
Jave.Lin 2009-12-12
  • 打赏
  • 举报
回复
mark study up
zhouxingyu896 2009-11-27
  • 打赏
  • 举报
回复
关注

帮定
lzhdim 2009-10-23
  • 打赏
  • 举报
回复
发一个ping的代码你参考下:

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();//关闭流
zxjdai 2009-10-05
  • 打赏
  • 举报
回复
这是个简单的例子,在button等空间的响应事件中调用这个方法就行了。

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();
}

chenmonuoyan 2009-10-05
  • 打赏
  • 举报
回复
有点不懂。。。。。。
APP开发王 2009-09-15
  • 打赏
  • 举报
回复
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();
mdmzl 2009-09-04
  • 打赏
  • 举报
回复
帮顶下。
b11ght 2009-09-04
  • 打赏
  • 举报
回复
....................
_小螃蟹_ 2009-09-04
  • 打赏
  • 举报
回复
没弄过,顶下吧
zhushoudong 2009-05-14
  • 打赏
  • 举报
回复
那就只有帮顶一下了
czk598478 2009-05-08
  • 打赏
  • 举报
回复
帮你顶上去
zhengyongxianglove 2009-05-07
  • 打赏
  • 举报
回复
请贴代码的朋友,注意,在B/S是可以的,在C/S里不行,



不要再贴相同的代码了。
zhengyongxianglove 2009-05-07
  • 打赏
  • 举报
回复
在C/S中,楼主给出的代码,一运行 就弹出一个一模一样的窗体,


无语。
zhengyongxianglove 2009-05-07
  • 打赏
  • 举报
回复
楼主 什么意思?
zgke 2009-05-06
  • 打赏
  • 举报
回复
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;
陌上花花 2009-05-04
  • 打赏
  • 举报
回复
帮顶下。

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧