ProcessStartInfo启动"cmd.exe"到运行"nvm"命令安装node版本弹出程序关联错误
ProcessStartInfo start "cmd.exe" to run "nvm" commands to install node versions pop up a program association error
我正在尝试使用带有控制台应用程序的 net core 2.0 自动执行机器设置,我需要 运行 一些 nvm 命令来配置节点版本。
我正在尝试使用我需要的 nvm 命令 运行 一个 .bat 文件,但我收到以下错误:
此文件没有关联的程序来执行此操作。请安装一个程序,或者如果已经安装了一个程序,请在默认程序控制面板中创建一个关联。
如果我直接从 cmd 执行 .bat 文件它工作正常,但是当我的控制台应用程序 运行 它我得到这个错误。
'file.bat' 命令是:
nvm version
nvm install 6.11.4
nvm use 6.11.4
nvm list
npm --version
我的 csharp 函数到 运行 命令:
public static int ExecuteCommand()
{
int exitCode;
ProcessStartInfo processInfo;
Process process;
processInfo = new ProcessStartInfo("cmd.exe", $"/C file.bat")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true
};
process = Process.Start(processInfo);
process.OutputDataReceived += (s, e) =>
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("cmd >" + e.Data);
Console.ResetColor();
};
process.BeginOutputReadLine();
process.ErrorDataReceived += (s, e) =>
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Data);
Console.ResetColor();
};
process.BeginErrorReadLine();
process.WaitForExit();
exitCode = process.ExitCode;
Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
process.Close();
return exitCode;
}
我的期望是让它工作,因为在那之后我将需要 运行 几个其他命令,如 npm install、gulp install 等
知道会发生什么吗?
纯粹基于测试,如果您更改此部分:
processInfo = new ProcessStartInfo("cmd.exe", $"/C file.bat")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true
};
不使用构造函数参数而是手动设置参数,例如:
processInfo = new ProcessStartInfo()
{
FileName = "cmd.exe",
Arguments = $"/C file.bat",
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true
};
应该可以解决问题。不确定为什么,因为从 ProcessStartInfo 上的 github 代码开始,构造函数仅接收参数并将它们存储在各自的属性(文件名和参数)中。
我正在尝试使用带有控制台应用程序的 net core 2.0 自动执行机器设置,我需要 运行 一些 nvm 命令来配置节点版本。
我正在尝试使用我需要的 nvm 命令 运行 一个 .bat 文件,但我收到以下错误:
此文件没有关联的程序来执行此操作。请安装一个程序,或者如果已经安装了一个程序,请在默认程序控制面板中创建一个关联。
如果我直接从 cmd 执行 .bat 文件它工作正常,但是当我的控制台应用程序 运行 它我得到这个错误。
'file.bat' 命令是:
nvm version
nvm install 6.11.4
nvm use 6.11.4
nvm list
npm --version
我的 csharp 函数到 运行 命令:
public static int ExecuteCommand()
{
int exitCode;
ProcessStartInfo processInfo;
Process process;
processInfo = new ProcessStartInfo("cmd.exe", $"/C file.bat")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true
};
process = Process.Start(processInfo);
process.OutputDataReceived += (s, e) =>
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("cmd >" + e.Data);
Console.ResetColor();
};
process.BeginOutputReadLine();
process.ErrorDataReceived += (s, e) =>
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(e.Data);
Console.ResetColor();
};
process.BeginErrorReadLine();
process.WaitForExit();
exitCode = process.ExitCode;
Console.WriteLine("ExitCode: " + exitCode.ToString(), "ExecuteCommand");
process.Close();
return exitCode;
}
我的期望是让它工作,因为在那之后我将需要 运行 几个其他命令,如 npm install、gulp install 等
知道会发生什么吗?
纯粹基于测试,如果您更改此部分:
processInfo = new ProcessStartInfo("cmd.exe", $"/C file.bat")
{
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true
};
不使用构造函数参数而是手动设置参数,例如:
processInfo = new ProcessStartInfo()
{
FileName = "cmd.exe",
Arguments = $"/C file.bat",
CreateNoWindow = true,
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardOutput = true
};
应该可以解决问题。不确定为什么,因为从 ProcessStartInfo 上的 github 代码开始,构造函数仅接收参数并将它们存储在各自的属性(文件名和参数)中。