gpt4 book ai didi

c# - 如何获取cmd的输出?

转载 作者:行者123 更新时间:2023-11-30 20:53:54 26 4
gpt4 key购买 nike

我想以字符串的形式获取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();
}

Error: An unhandled exception of type 'System.InvalidOperationException' occurred in System.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_DataReceivedprocess_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;
}

进程退出后,您仍然可以收到输出数据,因此如果您需要等待进程完成,您可以添加一个标志( bool 值)并仅在 process_DataReceived 收到空数据时将其设置为 false 。只有在发生这种情况时,您才能确定您已经收到流程的所有输出。

从“process_DataReceived”访问 UI

如果您必须从 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;
}

关于c# - 如何获取cmd的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19606327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com