我正在通过Process
以下命令安装MSI软件包:
msiexec.exe /norestart /qn /l*v! mylog.log /i package.msi
使用此C#代码:
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "msiexec";
p.StartInfo.Arguments = "/norestart /qn /l*v! mylog.log /i package.msi";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WorkingDirectory = workingDir;
p.Start();
string output = p.StandardOutput.ReadToEnd();
bool status = p.WaitForExit(timeout);
现在,我想运行此命令,但要使用start.exe
:
start /wait msiexec.exe /norestart /qn /l*v! mylog.log /i package.msi
但是现在当我运行此命令时:
// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "start";
p.StartInfo.Arguments = "/wait msiexec.exe /norestart /qn /l*v! mylog.log /i package.msi";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WorkingDirectory = workingDir;
p.Start();
string output = p.StandardOutput.ReadToEnd();
bool status = p.WaitForExit(timeout);
但是运行它时,我得到Exception
:系统找不到指定的文件。也尝试设置UseShellExecute
为,true
但随后我得到另一个Exception
:Process对象必须将UseShellExecute属性设置为false才能重定向IO流。
因此,可以start
使用C#运行命令吗?
尝试这样的事情。
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = @"/c start /wait msiexec.exe /norestart /qn /l*v! mylog.log /i package.msi";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WorkingDirectory = workingDir;
我认为您需要使用CMD来执行启动命令。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句