c#调用cmd并获取返回信息

yeaicc 2009-07-19 09:02:26
//实例化一个进程类
Process cmd = new Process();

//获得系统信息,使用的是 systeminfo.exe 这个控制台程序
cmd.StartInfo.FileName = "systeminfo.exe";

//将cmd的标准输入和输出全部重定向到.NET的程序里

cmd.StartInfo.UseShellExecute = false; //此处必须为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.Close();这句是关闭进程,那么是否cmd窗体(虽然它没显示)也关闭了?


请:
...全文
3691 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
qwestw 2011-11-01
  • 打赏
  • 举报
回复
有C++的没?
qianglc 2010-12-29
  • 打赏
  • 举报
回复
很奇怪。我在form里面调用上面的 方法,使用cmd的时候,居然只是弹出一个新窗口,这个怎么回事啊?
zxjdai 2009-10-05
  • 打赏
  • 举报
回复
把楼上的 SortOutputHandler函数中的
sortOutput.Append(Environment.NewLine +
"[" + numOutputLines.ToString() + "] - " + outLine.Data);
改为
Console.WriteLinev(Environment.NewLine +
"[" + numOutputLines.ToString() + "] - " + outLine.Data);

就可以简陋的实时显示了,可以试试哦。
yeaicc 2009-07-20
  • 打赏
  • 举报
回复
那我怎么样才可以在cmd进程不结束前就获得一部分返回信息呢?
哪怕是间隔性也好啊。
yeaicc 2009-07-20
  • 打赏
  • 举报
回复
那我怎么样才可以在cmd进程不结束前就获得一部分返回信息呢?
哪怕是间隔性也好啊。
yeaicc 2009-07-20
  • 打赏
  • 举报
回复

我查了下,希望如此:

.NET Framework 类库
Process..::.OutputDataReceived 事件

更新:2007 年 11 月

当应用程序写入其重定向 StandardOutput 流中时发生。

命名空间: System.Diagnostics
程序集: System(在 System.dll 中)

我查了下,希望如此:
语法:
[BrowsableAttribute(true)]
public event DataReceivedEventHandler OutputDataReceived


备注
OutputDataReceived 事件指示关联的 Process 已写入其重定向 StandardOutput 流中。

该事件在对 StandardOutput 执行异步读取操作期间启用。若要启动异步读取操作,必须重定向 Process 的 StandardOutput 流,向 OutputDataReceived 事件添加事件处理程序,并调用 BeginOutputReadLine。之后,每当该进程向重定向 StandardOutput 流中写入一行时,OutputDataReceived 事件都会发出信号,直到该进程退出或调用 CancelOutputRead 为止。

我查了下,希望如此:
示例:
// Define the namespaces used by this sample.
using System;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;
using System.ComponentModel;

namespace ProcessAsyncStreamSamples
{
class SortOutputRedirection
{
// Define static variables shared by class methods.
private static StringBuilder sortOutput = null;
private static int numOutputLines = 0;

public static void SortInputListText()
{
// Initialize the process and its StartInfo properties.
// The sort command is a console application that
// reads and sorts text input.

Process sortProcess;
sortProcess = new Process();
sortProcess.StartInfo.FileName = "Sort.exe";

// Set UseShellExecute to false for redirection.
sortProcess.StartInfo.UseShellExecute = false;

// Redirect the standard output of the sort command.
// This stream is read asynchronously using an event handler.
sortProcess.StartInfo.RedirectStandardOutput = true;
sortOutput = new StringBuilder("");

// Set our event handler to asynchronously read the sort output.
sortProcess.OutputDataReceived += new DataReceivedEventHandler(SortOutputHandler);

// Redirect standard input as well. This stream
// is used synchronously.
sortProcess.StartInfo.RedirectStandardInput = true;

// Start the process.
sortProcess.Start();

// Use a stream writer to synchronously write the sort input.
StreamWriter sortStreamWriter = sortProcess.StandardInput;

// Start the asynchronous read of the sort output stream.
sortProcess.BeginOutputReadLine();

// Prompt the user for input text lines. Write each
// line to the redirected input stream of the sort command.
Console.WriteLine("Ready to sort up to 50 lines of text");

String inputText;
int numInputLines = 0;
do
{
Console.WriteLine("Enter a text line (or press the Enter key to stop):");

inputText = Console.ReadLine();
if (!String.IsNullOrEmpty(inputText))
{
numInputLines ++;
sortStreamWriter.WriteLine(inputText);
}
}
while (!String.IsNullOrEmpty(inputText) && (numInputLines < 50));
Console.WriteLine("<end of input stream>");
Console.WriteLine();

// End the input stream to the sort command.
sortStreamWriter.Close();

// Wait for the sort process to write the sorted text lines.
sortProcess.WaitForExit();
感谢各位。
if (numOutputLines > 0)
{
// Write the formatted and sorted output to the console.
Console.WriteLine(" Sort results = {0} sorted text line(s) ",
numOutputLines);
Console.WriteLine("----------");
Console.WriteLine(sortOutput);
}
else
{
Console.WriteLine(" No input lines were sorted.");
}

sortProcess.Close();
}
感谢各位。
private static void SortOutputHandler(object sendingProcess,
DataReceivedEventArgs outLine)
{
// Collect the sort command output.
if (!String.IsNullOrEmpty(outLine.Data))
{
numOutputLines++;

// Add the text to the collected output.
sortOutput.Append(Environment.NewLine +
"[" + numOutputLines.ToString() + "] - " + outLine.Data);
}
}
}
}


权限
LinkDemand
用于完全信任直接调用方。此成员不能由部分受信任的代码使用。
我查了下,希望如此。
benzite 2009-07-19
  • 打赏
  • 举报
回复
你这是在启动另外一个进程,加上你的程序一共有两个进程,两个进程运行时有同步问题。红字就是用来同步的。你的程序在执行cmd.Close之前,必须一直等待systeminfo.exe结束才行。因为你的程序如果先结束了,那你打开的那个程序就成了野进程了,系统正常情况下永远不可能关闭它,除非人工操作或关机。

cmd.Close()如果关闭了,只能释放其托管的资源,好像那个进程关不掉。这类问题你自己动手实验一下不就行了。
benzite 2009-07-19
  • 打赏
  • 举报
回复
你这是在启动另外一个进程,加上你的程序一共有两个进程,两个进程运行时有同步问题。红字就是用来同步的。你的程序在执行cmd.Close之前,必须一直等待systeminfo.exe结束才行。因为你的程序如果先结束了,那你打开的那个程序就成了野进程了,系统正常情况下永远不可能关闭它,除非人工操作或关机。

cmd.Close()如果关闭了,只能释放其托管的资源,好像那个进程关不掉。这类问题你自己动手实验一下不就行了。
SQL77 2009-07-19
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 hzzasdf 的回复:]
线程是异步的,不能实时获取。


还有:cmd.Close();这句是关闭进程,那么是否cmd窗体(虽然它没显示)也关闭了?
=========
是的

[/Quote]
前来学习!!
yeaicc 2009-07-19
  • 打赏
  • 举报
回复
请问2楼的:


既然能实时获取,那么如何做呢?能否给出代码啊?
我想在窗体中实时显示返回信息的。
  • 打赏
  • 举报
回复
线程是异步的,不能实时获取。


还有:cmd.Close();这句是关闭进程,那么是否cmd窗体(虽然它没显示)也关闭了?
=========
是的
paulin 2009-07-19
  • 打赏
  • 举报
回复
cmd.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
设置了隐藏,Normal的话你就能看到了
cmd.Close()是关闭cmd的。
能实时获取,需要实现Process.OutputDataReceived 事件
但是你只是让Textbox显示执行最后结果,貌似没有必要
yeaicc 2009-07-19
  • 打赏
  • 举报
回复

红字部分如下:
只有等进程退出后才能真正拿到

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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