如何将值从控制台应用程序返回到 .NET 中的服务?

发布于 2024-08-08 10:07:57 字数 247 浏览 2 评论 0原文

我有一个.NET 服务。

我有一个 .NET 控制台应用程序。

我想要类似于调用 Process.Start("consoleapp.exe") 的服务并获取从应用程序返回的一些信息,最好只返回一个数字。

我该怎么做?

编辑:

我认为它必须是:Process.Start("myapp.exe").ExitCode - 但如何在控制台应用程序中设置退出代码?

I have a .NET service.

I have a .NET console application.

I want something along the lines of the service calling Process.Start("consoleapp.exe") and getting some information returned back from the app, ideally just returning a number.

How do I do this?

Edit:

I figure it must be: Process.Start("myapp.exe").ExitCode - but how do I set the exit code in the console app?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

顾挽 2024-08-15 10:07:57
Process p = Process.Start("app.exe");
p.WaitForExit();
int number = p.ExitCode;

在 app.exe 中设置 Enviroment.ExitCode...

Process p = Process.Start("app.exe");
p.WaitForExit();
int number = p.ExitCode;

And in app.exe you set Enviroment.ExitCode...

顾北清歌寒 2024-08-15 10:07:57

来使用退出代码 这

public void Main(string[] args){return;}

您可以通过将 Main 方法从 更改为

public int Main(string[] args){return 0;}

不是一个优雅的解决方案,但它有效。

you can make use of an Exit Code by changing your Main method from

public void Main(string[] args){return;}

to

public int Main(string[] args){return 0;}

Not an elegant solution, but it works.

锦欢 2024-08-15 10:07:57

可以通过标准 ExitCode 机制。您只需Start() 一个进程,然后WaitForExit(),然后获取ExitCode 的值。

An arbitrary number can be returned via standard ExitCode mechanism. You'd just have to Start() a process, then WaitForExit(), and then get the value of ExitCode.

时光倒影 2024-08-15 10:07:57

您可以使用退出代码,该代码可以从 Main 函数返回或通过 Environment.ExitCode 设置。

另一种选择是让 ConsoleApp 写入标准输出流,并将输出重定向到主应用程序。

第二个选项的优点是,如果以后您发现有必要的话,您可以返回比单个整数更多的数据。缺点是设置工作量比较大。

这篇 CodeProject 文章逐步介绍了如何重定向生成进程的标准输出流。

You can use the exit code, which can be returned from the Main function or set via Environment.ExitCode.

Another option would be to have your ConsoleApp write to the standard output stream, and redirect the output to your main application.

The advantage of the second option is that you could return more data than a single integer, if later you find this necessary. The disadvantage is that it's a bit more work to setup.

This CodeProject article walks through redirecting the standard output streams of a spawned process.

谜兔 2024-08-15 10:07:57

控制台应用程序可以设置其退出代码。

控制台应用可以通过以下任一方式设置其退出代码:

  1. 调用 System.Environment.Exit(exitcode),或
  2. main 函数返回 int。

ExitCode 属性(属于 System.Diagnostics.Process 类)允许服务进程检查控制台应用程序的退出代码。

The console app can set its exit code.

The console app can set its exit code by either:

  1. Call System.Environment.Exit(exitcode), or
  2. Let the main function return an int.

The ExitCode property (of the System.Diagnostics.Process class) lets the service process examine the console app's exit code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文