且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用dotnet core在Centos上执行linux命令

更新时间:2023-12-04 09:43:58

如果应用程序在Linux计算机上运行,​​那么您也可以尝试以下操作:

If application is running on Linux machine then you can try this also:

string command = "write your command here";
string result = "";
using (System.Diagnostics.Process proc = new System.Diagnostics.Process())
{
    proc.StartInfo.FileName = "/bin/bash";
    proc.StartInfo.Arguments = "-c \" " + command + " \"";
    proc.StartInfo.UseShellExecute = false;
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.RedirectStandardError = true;
    proc.Start();

    result += proc.StandardOutput.ReadToEnd();
    result += proc.StandardError.ReadToEnd();

    proc.WaitForExit();
}
return result;