如何获取 cmd 的输出
本文关键字:cmd 输出 获取 何获取 | 更新日期: 2023-09-27 18:37:00
我想在字符串中获取cmd结果的输出
void getDevices()
{
Process p = new Process();
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = @"C:'Users'mehmetcan'Desktop'adt-bundle-windows-x86-20130917'sdk'platform-tools'adb.exe";
proc.Arguments = @"devices";
Process.Start(proc);
String output = p.StandardOutput.ReadToEnd();
}
错误: 系统中发生"系统无效操作异常"类型的未处理异常.dll
你必须给你的进程一个ProcessStartInfo,告诉它你将读取输出。
下面是一个示例:
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = @"C:'Users'mehmetcan'Desktop'adt-bundle-windows-x86-20130917'sdk'platform-tools'adb.exe";
startinfo.Arguments = @"devices";
startinfo.RedirectStandardOutput = true;
startinfo.RedirectStandardError = true;
// Note: declare process as a variable in the class as it needs to be used in the event handlers
process = new Process();
process.StartInfo = startinfo;
process.OutputDataReceived += process_DataReceived;
process.ErrorDataReceived += process_DataReceived;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
然后是接收数据的事件处理程序(每次进程输出某些内容时都调用):
private void process_DataReceived(object sender, DataReceivedEventArgs e)
{
// null data means we've received everything
if (e.Data == null) {
process.CancelOutputRead();
process.CancelErrorRead();
return;
}
// e.Data is the string with the output from the process
Console.Write(e.Data);
}
请注意,每当进程输出某些内容时都会调用process_DataReceived
,如果您需要的是获得完整的输出,那么您可以这样做:
process_output = ""; // reset output before starting the process
ProcessStartInfo startinfo = new ProcessStartInfo();
startinfo.FileName = @"C:'Users'mehmetcan'Desktop'adt-bundle-windows-x86-20130917'sdk'platform-tools'adb.exe";
startinfo.Arguments = @"devices";
startinfo.RedirectStandardOutput = true;
startinfo.RedirectStandardError = true;
Process process = new Process();
process.StartInfo = startinfo;
process.OutputDataReceived += process_DataReceived;
process.ErrorDataReceived += process_DataReceived;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
// check end of answer if you need to wait for the process to terminate
声明一个对process_DataReceived
和process_Exited
都可用的字符串变量,例如包含方法的类中的变量:
string process_output; // will accumulate the output of the process
然后是事件处理程序:
private void process_DataReceived(object sender, DataReceivedEventArgs e)
{
// null data means we've received everything
if (e.Data == null) {
process.CancelOutputRead();
process.CancelErrorRead();
// do something with the output:
Console.Write(process_output);
return;
}
// append the output to the accumulator
process_output += e.Data;
}
进程退出后,您仍然可以接收输出数据,因此如果您需要等待进程完成,则可以添加一个标志(布尔值)并仅在收到 process_DataReceived
中的空数据时才将其设置为 false。发生这种情况时,您只能确保已收到流程的所有输出。
从"process_DataReceived"访问用户界面
如果必须从process_DataReceived
访问 UI 元素,则可以使用其调度程序来实现。下面是一个示例:
private void process_DataReceived(object sender, DataReceivedEventArgs e)
{
// null data means we've received everything
if (e.Data == null) {
process.CancelOutputRead();
process.CancelErrorRead();
// with WPF:
mylabel.Dispatcher.Invoke(new Action(() => {
mylabel.Content = process_output;
}));
// with WinForms
mylabel.Invoke((MethodInvoker) (() =>
{
mylabel.Text = process_output;
}));
return;
}
// append the output to the accumulator
process_output += e.Data;
}
像这样尝试怎么样:-
public static void Main()
{
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = @"C:'Users'mehmetcan'Desktop'adt-bundle-windows-x86-20130917'sdk'platform-tools'adb.exe";
proc.Arguments = @"devices";
Process p = Process.Start(proc);
p.OutputDataReceived += (sender, e) => Console.WriteLine(e.Data);
p.BeginOutputReadLine();
p.Start();
p.WaitForExit();
Thread.Sleep(5000);
}
另请检查 Process.OutputDataReceived 和 Process.BeginOutputReadLine