我编写了一个 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/