从控制台应用程序返回字符串

Returning a string from a console application(从控制台应用程序返回字符串)

本文介绍了从控制台应用程序返回字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真正想做的是这个

static string Main(string[] args)

但这不起作用,您唯一的选择是 voidint.那么,有哪些不同的方法可以返回我需要返回给调用应用程序的字符串?

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.

这篇关于从控制台应用程序返回字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:从控制台应用程序返回字符串

基础教程推荐