简体   繁体   中英

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?

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.

Idea 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.

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

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:

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

An article on 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM