Process.Start () throws an "Access Denied" error

When I execute the process 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 reproduction:

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();
}
+3
source share
3 answers

, , - . , - . , . , TaskMgr.exe, "" , , . , superuser.com

+3

, , , , process.start, .

H redirectOutput? , , , .

+2

, .

string path = "C:\\test.exe";
proc.StartInfo.FileName = path;

?

: , , .bat, "batch.bat", . , , .

+1

Source: https://habr.com/ru/post/1678436/


All Articles