【发布时间】:2012-01-22 19:44:07
【问题描述】:
当我执行一个进程并尝试重定向输出/错误时,我收到以下错误:
System.ComponentModel.Win32Exception (0x80004005): Access is denied
at System.Diagnostics.Process.CreatePipe(SafeFileHandle& parentHandle, SafeFileHandle& childHandle, Boolean parentInputs)
at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
...
可能出了什么问题?这是一个再现:
string path = "C:\\batch.cmd";
using (Process proc = new Process())
{
bool pathExists = File.Exists(path);
if(!pathExists) throw new ArgumentException("Path doesnt exist");
proc.StartInfo.FileName = path;
proc.StartInfo.WorkingDirectory = workingDir.FullName;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.Start(); //Exception thrown here
proc.WaitForExit();
}
【问题讨论】:
-
如果你这样做
Console.Writeline(File.Exists(path));会发生什么?同样的例外? -
您确定您可以访问“路径”中的文件吗?
-
proc.Start() 上抛出异常。如果我检查路径,它存在: bool pathExists = File.Exists(path); if(!pathExists) { throw new ArgumentException("路径不存在"); }