C# 如何从控制台应用程序获取和设置变量
我有一个.net应用程序,它使用以下代码调用.net核心应用程序:C# 如何从控制台应用程序获取和设置变量,c#,.net,.net-core,process,console-application,C#,.net,.net Core,Process,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; startIn
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();
正如您所见,我正在console应用程序的主方法上设置字符串(参数
),我得到的结果如下:
static void Main(string[] args)
{
var inputCommands = args[0].Split(",");
...
}
此外,我还捕获了Console.WriteLine()“事件”并执行一些操作。我在控制台应用程序中有一个方法,它返回bool类型。现在从.net应用程序中,我需要知道该方法返回了什么结果。我可以像Console.WriteLine(“方法返回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...
}
请同时共享.NET核心应用程序exe的返回值是退出代码。请参阅:^---this以及
System.Environment.Exit(您的exitcode)
要使用特定代码或static int Main(…){…return YourExitCode;}
@Cid确定,如何从进程中获取我的YourExitCode?@DIlshodK检查jdwend提供的链接