使用参数从 .NetCore 运行 bash 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44874439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
run bash command from .NetCore with arguments
提问by holocen
I am trying to run one .NetCore program from another.
我正在尝试从另一个运行一个 .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;
As output I get:
作为输出,我得到:
Microsoft .NET Core Shared Framework Host
Version : 1.1.0 Build : 928f77c4bc3f49d892459992fb6e1d5542cb5e86
Usage: dotnet [common-options] [[options] path-to-application]
Common Options: --help Display .NET Core Shared Framework Host help. --version Display .NET Core Shared Framework Host version.
Options: --fx-version Version of the installed Shared Framework to use to run the application.
--additionalprobingpath Path containing probing policy and assemblies to probe for.Path to Application: The path to a .NET Core managed application, dll or exe file to execute.
If you are debugging the Shared Framework Host, set 'COREHOST_TRACE' to '1' in your environment.
To get started on developing applications for .NET Core, install the SDK from: http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
Microsoft .NET Core 共享框架主机
版本:1.1.0 内部版本:928f77c4bc3f49d892459992fb6e1d5542cb5e86
用法:dotnet [common-options] [[options] path-to-application]
常用选项:--help 显示 .NET Core 共享框架主机帮助。--version 显示 .NET Core 共享框架主机版本。
选项:--fx-version 用于运行应用程序的已安装共享框架的版本。
--additionalprobingpath 包含要探测的探测策略和程序集的路径。应用程序路径:要执行的 .NET Core 托管应用程序、dll 或 exe 文件的路径。
如果您正在调试共享框架主机,请在您的环境中将“COREHOST_TRACE”设置为“1”。
要开始为 .NET Core 开发应用程序,请从以下位置安装 SDK:http: //go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
So I seems exacly like run command dotnetwith no dll path argument.
所以我看起来很像没有 dll 路径参数的运行命令dotnet。
回答by Martin Ullrich
You need to escape the argument to -c
so that it is one single argument:
您需要将参数转义-c
为一个参数:
psi.Arguments = "-c \"dotnet /home/myuser/PublishOutput/myprogram.dll\"";
回答by Linkanyway
i change something and make the message can be output sync
我改变了一些东西并使消息可以输出同步
using System;
using System.Diagnostics;
using System.Threading.Tasks;
namespace ExecuteCommandX
{
/// <summary>
/// sample by [email protected]
/// </summary>
/// <param name="args"></param>
internal static class Program
{
/// <summary>
///
/// </summary>
/// <param name="args"></param>
// ReSharper disable once UnusedParameter.Local
private static void Main(string[] args)
{
var psi = new ProcessStartInfo
{
FileName = "ping",
Arguments = "-c 3 8.8.8.8",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
var proc = new Process
{
StartInfo = psi
};
proc.Start();
Task.WaitAll(Task.Run(() =>
{
while (!proc.StandardOutput.EndOfStream)
{
var line = proc.StandardOutput.ReadLine();
Console.WriteLine(line);
}
}), Task.Run(() =>
{
while (!proc.StandardError.EndOfStream)
{
var line = proc.StandardError.ReadLine();
Console.WriteLine(line);
}
}));
proc.WaitForExit();
Console.WriteLine(proc.ExitCode);
}
}
}
}