如何从控制台应用程序获取和设置变量

How to get and set variables from console application

我有一个 .net 应用程序,它使用以下代码调用 .net 核心应用程序:

Process p = new Process();
var startInfo = p.StartInfo;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.FileName = "myDotNetCoreApp.exe";
startInfo.Arguments = arguments;
startInfo.CreateNoWindow = true;
p.OutputDataReceived += P_OutputDataReceived;
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();

如您所见,我在控制台应用程序的 Main 方法上设置了字符串 (argument),我是这样得到它的:

  static void Main(string[] args)
  {
    var inputCommands = args[0].Split(",");
    ...
  }

另外,我赶上 Console.WriteLine() "event" 并做了一些动作。我在控制台应用程序中有一个方法,它是 returns bool 类型。现在,在 .net 应用程序中,我需要知道该方法返回了什么结果。我可以像 Console.WriteLine("Method returned true") 那样做,但它不喜欢我。还有另一种方法吗?

来自@Cid 的评论:

在您的 myDotNetCoreApp 应用中,使用以下命令关闭您的应用:

System.Environment.Exit(0)

0 表示您的应用已成功关闭

然后使用它从 myDotNetCoreApp 获取信息

Process p = new Process();
var startInfo = p.StartInfo;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.FileName = "myDotNetCoreApp.exe";
startInfo.Arguments = arguments;
startInfo.CreateNoWindow = true;
p.OutputDataReceived += P_OutputDataReceived;
p.Start();
p.BeginOutputReadLine();
p.WaitForExit();
if(p.ExitCode==0){
//Do some...
}