我有一个安装程序(installer.exe
),可以调用另一个安装程序(subinstaller.exe
)。这是我触发其他安装程序的方式:
public void Run(String commandName, params String[] args)
{
var commandProcess = new Process();
commandProcess.StartInfo.FileName = "cmd.exe";
commandProcess.StartInfo.RedirectStandardInput = true;
commandProcess.StartInfo.RedirectStandardOutput = true;
commandProcess.StartInfo.RedirectStandardError = true;
commandProcess.StartInfo.CreateNoWindow = true;
commandProcess.StartInfo.UseShellExecute = false;
commandProcess.EnableRaisingEvents = true;
var arguments = String.Join(" ", args);
var command = String.Format("/C \"\"{0}\" {1}\"", commandName, arguments);
commandProcess.StartInfo.Arguments = command;
using (commandProcess)
{
commandProcess.Start();
var errors = commandProcess.StandardError.ReadToEnd();
if (String.IsNullOrEmpty(errors) == false)
{
var message = String.Format("A problem occurred while running \"{0}\": {1}", command, errors);
throw new Exception(message);
}
commandProcess.WaitForExit();
}
}
导致我麻烦的命令最终会出现以下情况:
cmd.exe /C ""C:\Updater\subinstaller.exe" /norestart /silent /serial="TRIAL""
如果我在命令提示符下运行该命令,一切都按预期工作。但是,如果我通过上述过程(commandName
为C:\Updater\subinstaller.exe
)运行它,那么它就无法正常运行。我认为这是因为许可问题,但我已经有效地排除了这一点:
cmd.exe
进程是在相同的提升权限下运行的。我真的想知道可能导致问题的原因。考虑到它在原始命令行中工作,它不太可能是子安装程序。有没有人有任何想法?
更新
我还尝试将cmd
排除在等式之外:
public void Run(String commandName, params String[] args)
{
var commandProcess = new Process();
commandProcess.StartInfo.FileName = commandName;
commandProcess.StartInfo.RedirectStandardInput = true;
commandProcess.StartInfo.RedirectStandardOutput = true;
commandProcess.StartInfo.RedirectStandardError = true;
commandProcess.StartInfo.CreateNoWindow = true;
commandProcess.StartInfo.UseShellExecute = false;
commandProcess.EnableRaisingEvents = true;
var arguments = String.Join(" ", args);
commandProcess.StartInfo.Arguments = arguments;
using (commandProcess)
{
commandProcess.Start();
var errors = commandProcess.StandardError.ReadToEnd();
if (String.IsNullOrEmpty(errors) == false)
{
var message = String.Format("A problem occurred while running \"{0}\": {1}", commandName, errors);
throw new Exception(message);
}
commandProcess.WaitForExit();
if (commandProcess.ExitCode != 0)
{
var message = String.Format("A problem occurred while running \"{0}\": Exit code {1}", commandName, commandProcess.ExitCode);
throw new Exception(message);
}
}
}