
Question:
When I execute a process through and try to redirect the output/error, I get the following error:
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)
...
What could be wrong? Here is a repro:
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();
}
Answer1:No decent reason for this to fail, the code has not yet gotten to a point where it would do anything security-sensitive. This is environmental, something on your machine is interfering. Reboot first, disable anti-malware next. If that doesn't help then use TaskMgr.exe, Processes tab and arbitrarily start killing processes, with some luck you'll hit the evil-doer. Ask questions about getting this machine stable again at superuser.com
Answer2:You have to make sure that the account that execute your program have the rights to execute the program your trying to launch with the process.start, and that the account have the rights to create a pipe on the system .
HAve you tried to remove the redirectOutput ? If without redirecting the output you dont get the exception means that your user can't create a pipe, so you have to give this right to the user .
Answer3:This should have the full file path and file name, trying to start a folder will result in this error.
string path = "C:\\test.exe";
proc.StartInfo.FileName = path;
Also does the application have administrative privileges?
<strong>Edit: if it is a batch file, it needs to have the extension .bat such as "batch.bat" to be run properly. Also if it is a batch file, it cannot be empty or else it will throw an exception.</strong>