当进程在单独的 *.dll 文件中创建时,我很难将控制台应用程序的输出重定向到 Windows 窗体应用程序(请原谅草率的术语,但我是编程新手)。我遇到了这个链接(我可能会追求他的方法),详细说明了一个类似的问题:http ://www.codeproject.com/KB/threads/launchprocess.aspx?msg=3087118我可以从控制台读取一行,但是如何让它保持开放?
所以基本上我的问题是如何从一个单独的类中正确访问流读取器输出?
下面的代码在本地调用时完美运行。
private void exampleErrorRedirection()
{
Process proc = new Process();
proc.StartInfo.FileName = /exampleconsoleapp.exe;
proc.StartInfo.Arguments = "some arguments that work";
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
if (!proc.Start())
{
Console.WriteLine("Error starting");
return;
}
StreamReader reader = proc.StandardError;
string line;
while ((line = reader.ReadLine()) != null)
{
textBoxForStandardError.Text = line;
}
proc.Close();
}
但是,我希望能够在调用时将输出从单独的类重定向。Atm 我只能从控制台获取第一行,并且它不会更新。
private void exampleErrorRedirection()
{
exampleDLLFile.startProc ConsoleApp new exampleDLLFile.startProc();
ConsoleApp.Run();
while (convert.line != null)
{
textBoxForStandardError.Text = ConsoleApp.line;
}
}
该类包含这样的方法:
public class convertFile
{
public string line;
public void Run()
{
Process proc = new Process();
proc.StartInfo.FileName = /exampleconsoleapp.exe;
proc.StartInfo.Arguments = "some arguments that work";
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
if (!proc.Start())
{
Console.WriteLine("Error starting");
}
StreamReader reader = proc.StandardError;
while ((line = reader.ReadLine()) != null)
{
line = reader.ReadLine();
}
proc.Close();
}
}