我正在从 Windows 窗体应用程序调用控制台应用程序。我想从控制台应用程序中获取字符串列表。 这是我的简化代码...
[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;
}
最佳答案
这样你就不行了。 Main 只能返回 void 或 int。 但是您可以将列表发送到标准输出并在另一个应用程序中读取它。
在控制台应用程序中添加:
Console.WriteLine(JsonConvert.SerializeObject(returnValues));
在来电应用中:
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();
关于c# - 如何从控制台应用程序返回 List<string>?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36590736/