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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19774155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:51:01  来源:igfitidea点击:

Returning a string from a console application

c#console

提问by Nevyn

What I really want to do is this

我真正想做的是这个

static string Main(string[] args)

but that doesn't work, your only options are voidand int. So, What are some different ways to return the string that I need to return to the calling application?

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

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?

据我所知,此时我唯一的选择是让调用应用程序在启动之前将侦听流附加到进程的标准输出流,并使用 Console.Out.Write 从内部发送“返回”我的可执行文件。这实际上是做到这一点的唯一方法,还是我可以使用不同/更好的东西?

采纳答案by Reed Copsey

Is this in fact the ONLY way to do this, or is there something different/better I can use?

这实际上是做到这一点的唯一方法,还是我可以使用不同/更好的东西?

This isn't the onlyway 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, andyou 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.

请注意,如果调用应用程序是 .NET 应用程序,并且您可以控制这两个应用程序,那么只编写类库而不是控制台应用程序可能更有意义。这将允许您将代码完全分开,但让可执行文件“调用”您的库以获取字符串数据。

回答by Joe

Idea 1:

想法1:

Using MyCode.exe, create an encrypted text file, which is saved in a specified path, which can then be decrypted in the current app and read.

使用MyCode.exe,创建一个加密的文本文件,保存在指定的路径下,然后可以在当前应用程序中解密并读取。

In the app: "MyCode.exe", add this code:

在应用程序“MyCode.exe”中,添加以下代码:

public void ReturnToOther()
{
    string ToReturn = "MyString";
    System.IO.File.WriteAllText("Path", Encrypt(ToReturn));
}

public String Encrypt(string ToEncrypt)
{
    string Encrypted = null
    char[] Array = ToEncrypt.ToCharArray();
    for (int i = 0; i < Array.Length; i++)
    {
        Encrypted += Convert.ToString(Convert.ToChar(Convert.ToInt32(Array[i]) + 15));
    }
    return Encrypted;
}

In the app you are making now:

在您现在制作的应用程序中:

public void GetString()
{
    string STR = Decrypt(System.IO.File.ReadAllText("Path"));
    Console.WriteLine("The string is: {0}", STR);
}

// If you want to keep this running before the file exists, use this:

/*
public void GetString()
{
    for(int i = 0; i > -1; ++i)
    {
        if(System.IO.File.Exists("Path"))
        {
            string STR = Decrypt(System.IO.File.ReadAllText("Path"));
            Console.WriteLine("The string is: {0}", STR);
            break;
        }
        else
        {
            //Do something if you want
        }
    }
} */


public String Decrypt(string ToDecrypt)
{
    string Decrypted = null
    char[] Array = ToDecrypt.ToCharArray();
    for (int i = 0; i < Array.Length; i++)
    {
        Decrypted += Convert.ToString(Convert.ToChar(Convert.ToInt32(Array[i]) - 15));
    }
    return Decrypted;
}

Idea 2:

想法2:

Use TCP to upload the string to a port, e.g. LocalHost (127.0.0.1), and then receive the string on the app you are developing, using a TCP Listener

使用 TCP 将字符串上传到端口,例如 LocalHost (127.0.0.1),然后使用 TCP 侦听器在您正在开发的应用程序上接收字符串

An article on TCP - http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx

一篇关于 TCP 的文章 - http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.aspx

Hope this helps :)

希望这可以帮助 :)

EDIT:

编辑:

Have a look at Sockets too: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx

也看看套接字:http: //msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx