Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

c# - Returning a string from a console application

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...