C# Process.Start()抛出一个;“拒绝访问”;错误

C# Process.Start()抛出一个;“拒绝访问”;错误,c#,redirect,process,C#,Redirect,Process,当我通过执行一个进程并尝试重定向输出/错误时,我得到以下错误: System.ComponentModel.Win32Exception (0x80004005): Access is denied at System.Diagnostics.Process.CreatePipe(SafeFileHandle& parentHandle, SafeFileHandle& childHandle, Boolean parentInputs) at System.Diagnost

当我通过执行一个进程并尝试重定向输出/错误时,我得到以下错误:

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();
}

这应该有完整的文件路径和文件名,尝试启动文件夹将导致此错误

string path = "C:\\test.exe";
proc.StartInfo.FileName = path;
应用程序是否也具有管理权限


编辑:如果是批处理文件,则需要具有扩展名.bat(如“batch.bat”)才能正常运行。此外,如果它是批处理文件,则不能为空,否则将引发异常。

您必须确保执行程序的帐户有权执行您尝试使用process.start启动的程序,并且该帐户有权在系统上创建管道


您是否尝试删除重定向输出?如果不重定向输出,您不会得到异常,这意味着您的用户无法创建管道,因此您必须将此权限授予用户。

没有合理的原因导致此操作失败,代码尚未达到可以执行任何安全敏感操作的程度。这是环境问题,你的机器上有干扰。首先重新启动,然后禁用反恶意软件。如果这没有帮助,那么使用TaskMgr.exe、进程选项卡并任意开始杀死进程,幸运的话,你会击中作恶者。在superuser.com上询问有关重新稳定此机器的问题

如果执行
Console.Writeline(File.Exists(path)),会发生什么?相同的异常?您确定有权访问“路径”中的文件吗?该异常在proc.Start()上引发。如果我检查路径,它是存在的:bool pathExists=File.exists(path);如果(!pathExists){抛出新的ArgumentException(“路径不存在”);}如果我将RedirectStandardError和RedirectStandardOutput设置为false,一切正常。我怎样才能授予用户创建管道的权限?试着以管理员的身份运行该程序,看看会发生什么。我遇到了完全相同的问题。转到我试图生成的EXE所在的目录,并使用read/execute将我正在运行的帐户添加到security选项卡,问题就消失了。