问题
I am trying to run one .NetCore program from another.
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
So I seems exacly like run command dotnet with no dll path argument.
回答1:
You need to escape the argument to -c
so that it is one single argument:
psi.Arguments = "-c \"dotnet /home/myuser/PublishOutput/myprogram.dll\"";
回答2:
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 linkanyway@gmail.com
/// </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);
}
}
}
来源:https://stackoverflow.com/questions/44874439/run-bash-command-from-netcore-with-arguments