c# - 从 C# 应用程序中使用命令行程序

标签 c# c++ command-line-interface

我编写了一个 C++ 程序(从命令行执行),运行良好。现在我需要将它用于我的 C# 应用程序。也就是说,我希望在调用 C# 应用程序时使用我的 C++ 程序的输出。

这可能吗?如果是,怎么办?

任何链接或帮助将不胜感激。

最佳答案

您可以使用 System.Diagnostics.Process 启动您的 C++ 程序并将其输出重定向到一个流,以便在您的 C# 应用程序中使用。 this question中的信息详细说明:

string command = "arg1 arg2 arg3"; // command line args
string exec = "filename.exe";      // executable name
string retMessage = String.Empty;
ProcessStartInfo startInfo = new ProcessStartInfo();
Process p = new Process();

startInfo.CreateNoWindow = true;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardInput = true;

startInfo.UseShellExecute = false;
startInfo.Arguments = command;
startInfo.FileName = exec;

p.StartInfo = startInfo;
p.Start();

using (StreamReader output = p.StandardOutput)
{
    retMessage = output.ReadToEnd();
}

p.WaitForExit();

return retMessage;

关于c# - 从 C# 应用程序中使用命令行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4801926/

相关文章:

c# - 如何使用 XML-RPC 在 Python 和 C# 之间进行通信?

c++ - 用于显示带有小图片(图标/表情符号)的文本的 QWidget

c++ - 我如何使用与 QVariant 相关的 QComboBox?

c# - 带有 dnx 项目的 EF 6

c# - Enum.GetValues() 的内部工作

c++ - 在作为函数参数传递的指针上使用 delete

javascript - 如何在 ShellJS 命令执行期间运行 cli-spinner?

ruby - Rest-Client gem RoR,获取 SSL 错误版本错误

c++ - gcnew Image CLI/C++ 时的无效操作

c# - 如何将 RichTextBox 滚动到底部?