我有以下 C# 代码行,其中打开进程并运行 dotnet 命令来打开我的控制台应用程序(使用 .net 标准/核心创建)
var args = new Dictionary<string, string> {
{ "-p", "title"},
{ "-l", "games"},
...
};
var arguments = string.Join(" ", args.Select((k) => string.Format("{0} {1}", k.Key, "\"" + k.Value + "\"")));
var dllPath = @"C:\Users\xyz\Documents\Visual Studio 2017\myConsole\bin\Debug\netcoreapp2.1\myConsole.dll";
ProcessStartInfo procStartInfo = new ProcessStartInfo();
procStartInfo.FileName = "C:\....\cmd.exe";
procStartInfo.Arguments = $"dotnet \"{dllPath}\" {arguments}";
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = false;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
StringBuilder sb = new StringBuilder();
Process pr = new Process();
pr.StartInfo = procStartInfo;
pr.OutputDataReceived += (s, ev) =>
{
if (string.IsNullOrWhiteSpace(ev.Data))
{
return;
}
string[] split = ev.Data.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
int.TryParse(split[split.Length - 1], out output);
};
pr.ErrorDataReceived += (s, err) =>
{
// do stuff here
};
pr.EnableRaisingEvents = true;
pr.Start();
pr.BeginOutputReadLine();
pr.BeginErrorReadLine();
pr.WaitForExit();
Run Code Online (Sandbox Code Playgroud)
命令Arguments结果为:
dotnet "C:\Users\xyz\Documents\Visual Studio 2017\myConsole\bin\Debug\netcoreapp2.1\myConsole.dll" -p "title" -l "games" -s "" -r "none" -k "0" -d "/path/" -f ""
Run Code Online (Sandbox Code Playgroud)
但是对于ev.DatafromOutputDataReceived事件看起来像:
Microsoft Windows [Version 10.0.16299.665]
(c) 2017 Microsoft Corporation. All rights reserved.
Run Code Online (Sandbox Code Playgroud)
就这样...
我希望对 dll 运行 dotnet 命令。
如果我手动运行dotnet ....上面的结果命令,则工作正常。但不是来自我的 C# 代码。为什么 ?
因为 cmd 返回:
Microsoft Windows [Version 10.0.16299.665]
(c) 2017 Microsoft Corporation. All rights reserved.
Run Code Online (Sandbox Code Playgroud)
你需要打电话
procStartInfo.FileName = "dotnet"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2620 次 |
| 最近记录: |