我正在尝试移植一个在 Visual Studio C# 下开发的 GUI 应用程序,以便在 Linux 上的 Wine 下运行。我遇到了 Process 类的问题。以下程序在使用 mcs 编译并使用 mono 运行时按预期工作:
using System.Diagnostics;
using System;
class TestProcess {
static void Main() {
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = "/usr/bin/find";
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.StartInfo.Arguments = "/sys";
process.ErrorDataReceived += new DataReceivedEventHandler(output);
process.OutputDataReceived += new DataReceivedEventHandler(output);
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
}
static void output(object sender, DataReceivedEventArgs e) {
Console.WriteLine(e.Data);
}
}
但是当我使用 wine 运行它时(我已经在 Wine 下安装了 Mono for Windows),它会失败并出现以下异常:
Unhandled Exception: System.InvalidOperationException: Standard error has not been redirected or process has not been started.
at System.Diagnostics.Process.BeginErrorReadLine () [0x00000] in <filename unknown>:0
at (wrapper remoting-invoke-with-check) System.Diagnostics.Process:BeginErrorReadLine ()
at TestProcess.Main () [0x00000] in <filename unknown>:0
我究竟做错了什么?