在Linux上c#如何调用外部程序

调用外部程序

其实和Windows相差不大,只需稍微改动一下,就可以使用.

1. 使用Process类来实现

ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.FileName = "sh";
psi.Arguments = string.Format("-c \"{0}\"", "dotnet -V");  //-c 后跟执行外部程序名和参数

using (Process p = Process.Start(psi))
{
    p.WaitForExit();
    int ret = p.ExitCode;
    if (ret != 0)
    {
       Console.WriteLine(ret);
    }
}

2. 使用P/Invoke,调用system执行外部程序

[DllImport("libc", SetLastError = true)]
private static extern int system(string cmd);

static void Main(string[] args)
{
    system("mono -V");   //里面传入外部程序名称和参数
}
秋风 2017-12-25