如何使用 C# 运行 并从另一个应用程序获取输出
How to run and get output from another application using C#
总结:
我有一个 WinForm 应用程序做一些工作并用 System.Console
写一些输出,这是代码:
static int Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Console.WriteLine("Some String");
return 1;
}
假设我们将其命名为SubApp.exe
在另一个程序中,我想执行此 subApp.exe
并读取它使用 System.Diagnostics.Process
创建的输出。这是我的代码:
System.Diagnostics.Process p = System.Diagnostics.Process.Start("SubApp.exe", "Some Parameter");
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
int exitCode = p.ExitCode;
问题:
但不幸的是,output
和 error
都是空的,更糟糕的是 exitCode
是 0
。问题是什么?我的程序是否正确?
让你的 SubApp
将参数写入控制台,然后它将 return 控制台输出到进程输出
static int Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Console.WriteLine(args[0]); // this will print only 'Some' part of argument all string will be in args seperating by space
Console.WriteLine(args[1]); // this will print "Parameter"
return 1;
}
然后打电话给你的SubApp
System.Diagnostics.Process p = System.Diagnostics.Process.Start("SubApp.exe", "Some Parameter");
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
int exitCode = p.ExitCode;
执行此操作后,您将 Some Parameter
放入 output
变量
总结:
我有一个 WinForm 应用程序做一些工作并用 System.Console
写一些输出,这是代码:
static int Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Console.WriteLine("Some String");
return 1;
}
假设我们将其命名为SubApp.exe
在另一个程序中,我想执行此 subApp.exe
并读取它使用 System.Diagnostics.Process
创建的输出。这是我的代码:
System.Diagnostics.Process p = System.Diagnostics.Process.Start("SubApp.exe", "Some Parameter");
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
p.WaitForExit();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
int exitCode = p.ExitCode;
问题:
但不幸的是,output
和 error
都是空的,更糟糕的是 exitCode
是 0
。问题是什么?我的程序是否正确?
让你的 SubApp
将参数写入控制台,然后它将 return 控制台输出到进程输出
static int Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Console.WriteLine(args[0]); // this will print only 'Some' part of argument all string will be in args seperating by space
Console.WriteLine(args[1]); // this will print "Parameter"
return 1;
}
然后打电话给你的SubApp
System.Diagnostics.Process p = System.Diagnostics.Process.Start("SubApp.exe", "Some Parameter");
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.Start();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
int exitCode = p.ExitCode;
执行此操作后,您将 Some Parameter
放入 output
变量