Returning a string from a console application(从控制台应用程序返回字符串)
问题描述
我真正想做的是这个
static string Main(string[] args)
但这不起作用,您唯一的选择是 void
和 int
.那么,有哪些不同的方法可以返回我需要返回给调用应用程序的字符串?
but that doesn't work, your only options are void
and int
. So, What are some different ways to return the string that I need to return to the calling application?
背景
我需要编写一个专门为从另一个应用程序调用而设计的控制台应用程序
I need to write a console app that is specifically designed to be called from another application
Process.Start("MyCode.exe -Option 12aaa1234");
这个调用程序如何接收从那个可执行文件返回的字符串?
How can this calling program receive a string returned from that executable?
研究
据我所知,此时我唯一的选择是让调用应用程序在启动之前将侦听流附加到进程的标准输出流,然后使用 Console.Out 发送返回".从我的可执行文件中写入.这实际上是做到这一点的唯一方法,还是我可以使用不同/更好的方法?
From what I can tell, at this point in time my only option is to have the calling application attach a listening stream to the Standard Output stream of the process before starting it, and send the "return" using Console.Out.Write from inside my executable. Is this in fact the ONLY way to do this, or is there something different/better I can use?
推荐答案
这实际上是唯一的方法吗,还是我可以使用不同/更好的方法?
Is this in fact the ONLY way to do this, or is there something different/better I can use?
这不是唯一的方法,但它是最常见的.
This isn't the only way to do this, but it is the most common.
其他选项将涉及某种形式的 interprocess通信,对于单个字符串,这可能会大大增加开发工作量.
The other options would involve some form of interprocess communication, which is likely going to be significantly more development effort for a single string.
请注意,如果调用应用程序是 .NET 应用程序,并且您可以控制这两个应用程序,那么编写类库而不是控制台应用程序可能更有意义.这将允许您将代码完全分开,但让可执行文件调用"您的库以获取字符串数据.
Note that, if the calling application is a .NET application, and you have control over both applications, it might make more sense to just write a class library instead of a console application. This would allow you to keep the code completely separate, but have the executable "call into" your library to get the string data.
这篇关于从控制台应用程序返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从控制台应用程序返回字符串
基础教程推荐
- 将 XML 转换为通用列表 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- c# Math.Sqrt 实现 2022-01-01