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

huangapple go评论77阅读模式
英文:

How to get and set variables from console application

问题

我有一个.NET应用程序,它使用以下代码调用一个.NET Core应用程序:

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方法中设置了字符串(arguments),并像这样获取它:

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

此外,我捕获了Console.WriteLine()的“事件”并执行一些操作。我在控制台应用程序中有一个方法,它返回bool类型。现在我需要从.NET应用程序中知道该方法返回了什么结果。我可以这样做:Console.WriteLine("Method returned true"),但这种方法不太符合我的需求。是否有其他方法可以实现这个目标?

英文:

I have a .net app that calls a .net core app using the following code:

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();

As you can see I'm setting strings (argument) on the Main method of console app and I'm getting it like this:

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

Also, I catch Console.WriteLine() "event" and doing some actions. I have a method in the console app and it returns bool type. Now from the .net app, I need to know what result was returned from the method. I could do it like Console.WriteLine("Method returned true") but it doesn't like me. Is there another way to do this?

答案1

得分: 1

从 @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){
    // 做一些操作...
}
英文:

From @Cid's comment:

In your myDotNetCoreApp app close your app using:

System.Environment.Exit(0)

0 means your app closed successfully

Then use this to get info from 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...
}

huangapple
  • 本文由 发表于 2020年1月6日 21:12:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/59612763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定