c# - 如何从控制台应用程序返回 List<string>?

标签 c# console-application program-entry-point

我正在从 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/

相关文章:

c++ - 将 C++ OpenGL 项目与另一个 C++ 项目集成

c# - 从其他应用程序 C# 获取按键

c# - 将位图转换为 UnmanagedImage

c# - 将此代码从 C# 转换为 VB.NET

java - 无法执行 jar 文件 : "no main manifest attribute"

java - NetBeans IDE,设置命令行参数并作为主项目运行,但是没有显示任何内容

c# - 如何从网址下载图片

c# - 在 C# 控制台应用程序中获取 google 驱动程序中文件的 downloadUrl

c# - 为 C# 控制台应用程序创建安装文件

c# - Visual Studio (MAC) 控制台应用程序