Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning a string from a console application

Tags:

c#

console

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?

like image 211
Nevyn Avatar asked Nov 04 '13 18:11

Nevyn


People also ask

Can main return string in C#?

No, we can't return string from Main method. The entry point can optionally only return an int value. As, this return value is used in application termination (§10.2). or use void Main if you dont want to return. Save this answer.

Can Main method return a value in c#?

The main method can also return a value. In programming languages, we have always seen the main() method return type is void. But it can be “int” also.

How do you exit a console program in C#?

You can use Environment. Exit(0) and Application. Exit .


2 Answers

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.

like image 183
Reed Copsey Avatar answered Sep 20 '22 07:09

Reed Copsey


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, e.g. 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

like image 32
Joe Avatar answered Sep 20 '22 07:09

Joe