从控制台应用程序返回一个字符串 [英] Returning a string from a console application

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

问题描述

我真正想要做的就是这个。



 静态字符串的主要(字串[] args)

但是,这并不正常工作,你唯一的选择是无效 INT 。那么,有哪些不同的方式来回报,我需要返回到调用应用程序的字符串?



背景



我需要编写专门从另一个应用程序调用一个控制台应用程序

 的Process.Start(MyCode.exe -Option 12aaa1234); 

这调用程序怎样才能收到一个字符串从可执行回来了?



研究



据我所知,在这个时间点我唯一的选择是让调用应用程序在开始之前,附上一个监听流过程的标准输出流,并从我的可执行文件中发送回归使用Console.Out.Write。这其实是做的唯一方法,或者是有什么不同/更好,我可以使用?


解决方案

这其实是唯一的办法做到这一点,或者是有什么不同/更好我可以使用?




这是不是的只有的方式做到这一点,但它是最常见的。



其他选项会涉及某种形式的进程间通信,这很可能将成为一个字符串显著更多的开发工作。



需要注意的是,如果调用应用程序是一个.NET应用程序,您对两个应用程序的控制,它可能会更有意义,只写一个类库,而不是一个控制台应用程序。这将让你保持代码完全分开的,但可执行调入资料库,以获得字符串数据。


What I really want to do is this

static string Main(string[] args)

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?

Background

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?

Research

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.

The other options would involve some form of interprocess communication, which is likely going to be significantly more development effort for a single string.

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.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆