Reputation: 389
I am calling a console application from a Windows forms application. I want to get a list of strings back from the console application. This is my simplified code...
[STAThread]
static List<string> Main(string[] args)
{
List<string> returnValues = new List<string>();
returnValues.Add("str_1");
returnValues.Add("str_2");
returnValues.Add("str_3");
return returnValues;
}
Upvotes: 2
Views: 5564
Reputation: 37
Main method is not made for this. But if you want to print your list here is the code :
public void showList(List<String> list)
{
foreach (string s in list)
{
Console.WriteLine(s);
}
}
Upvotes: 0
Reputation: 5764
In this way you cannot. Main can return only void or int. But you can send list to standard output and read it in another app.
In console app add this:
Console.WriteLine(JsonConvert.SerializeObject(returnValues));
And in caller app:
Process yourApp= new Process();
yourApp.StartInfo.FileName = "exe file";
yourApp.StartInfo.Arguments = "params";
yourApp.StartInfo.UseShellExecute = false;
yourApp.StartInfo.RedirectStandardOutput = true;
yourApp.Start();
string output = yourApp.StandardOutput.ReadToEnd();
List<string> list = JsonConvert.DeserializeObject<List<string>>(output);
yourApp.WaitForExit();
Upvotes: 6
Reputation: 156978
You can't just return a list, you have to serialize it in a way the other end can pick it up.
One option would be to serialize the list to JSON and send it over the Console.Out
stream. Then, on the other end, read from the process' output stream and deserialize it.
Upvotes: 6
Reputation: 24579
No, you cannot return a string or list of string. The Main method can only return void
or int
See MSDN
Upvotes: 2