使用参数从.NetCore运行bash命令

hol*_*cen 5 .net c# bash sh .net-core

我试图从另一个运行一个.NetCore程序.

        ProcessStartInfo psi = new ProcessStartInfo();
        psi.FileName = "sh";
        psi.Arguments = "-c dotnet /home/myuser/PublishOutput/myprogram.dll";
        psi.UseShellExecute = false;
        psi.RedirectStandardOutput = true;
        psi.RedirectStandardError = true;

        Process proc = new Process
        {
            StartInfo = psi
        };


        proc.Start();

        string error = proc.StandardError.ReadToEnd();

        if (!string.IsNullOrEmpty(error))
            return "error: " + error;

        string output = proc.StandardOutput.ReadToEnd();

        proc.WaitForExit();

        return output;
Run Code Online (Sandbox Code Playgroud)

作为输出我得到:

Microsoft .NET核心共享框架主机

版本:1.1.0建立:928f77c4bc3f49d892459992fb6e1d5542cb5e86

用法:dotnet [common-options] [[options] path-to-application]

常见选项: - help显示.NET Core共享框架主机帮助.--version显示.NET Core共享框架主机版本.

选项: - fx-version用于运行应用程序的已安装Shared Framework的版本.
--additionalprobingpath包含要探测的探测策略和程序集的路径.

应用程序的路径:.NET Core托管应用程序的路径,要执行的dll或exe文件.

如果要调试Shared Framework Host,请在环境中将"COREHOST_TRACE"设置为"1".

要开始开发.NET Core应用程序,请从以下位置安装SDK:http: //go.microsoft.com/fwlink/?LinkID = 798306&clcid = 0x409

所以我似乎非常喜欢运行命令dotnet而没有dll路径参数.

Mar*_*ich 5

您需要将参数转义-c为一个参数:

psi.Arguments = "-c \"dotnet /home/myuser/PublishOutput/myprogram.dll\"";
Run Code Online (Sandbox Code Playgroud)