110,534
社区成员
发帖
与我相关
我的任务
分享
//实例化一个进程类
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();//关闭该进程
[BrowsableAttribute(true)]
public event DataReceivedEventHandler OutputDataReceived
// 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);
}
}
}
}