4

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();
}
Gjorgji
  • 22,458
  • 10
  • 31
  • 39
  • What happens if you do `Console.Writeline(File.Exists(path));`? Same exception? – tomfanning Dec 17 '11 at 22:45
  • Are you sure you have access to the file in 'path'? – M.Babcock Dec 17 '11 at 22:45
  • The exception is thrown on proc.Start(). If I check for the path, it exists: bool pathExists = File.Exists(path); if(!pathExists) { throw new ArgumentException("Path doesnt exist"); } – Gjorgji Dec 17 '11 at 22:49

3 Answers3

3

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

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
2

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 .

aleroot
  • 71,077
  • 30
  • 176
  • 213
  • If I set RedirectStandardError and RedirectStandardOutput to false everything works fine. How can I give a right to a user to create a pipe? – Gjorgji Dec 17 '11 at 22:52
  • Try to run that program as administrator and see what happens. – aleroot Dec 17 '11 at 22:53
  • I had this exact same problem. Went to the directory where the EXE I was trying to spawn was and added the account I was running as to the security tab with read/execute and the problem went away. – Ken Forslund Oct 29 '15 at 13:43
0

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?

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.

John
  • 5,942
  • 3
  • 42
  • 79
  • This is not true - a batch file can have the extension "cmd" and still run fine. There is a *slight* difference, but not a huge one – derekantrican Aug 12 '21 at 12:54