【问题标题】:Process.Start() throws an "Access Denied" errorProcess.Start() 抛出“拒绝访问”错误
【发布时间】: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("路径不存在"); }

标签: c# redirect process


【解决方案1】:

没有正当的理由失败,代码还没有达到可以做任何安全敏感的事情的地步。这是环境问题,您机器上的某些东西正在干扰。首先重新启动,然后禁用反恶意软件。如果这没有帮助,那么使用TaskMgr.exe,进程选项卡并任意开始杀死进程,如果运气好的话,你会遇到坏人。在 superuser.com 上询问有关让这台机器再次稳定的问题

【讨论】:

    【解决方案2】:

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

    您是否尝试过删除 redirectOutput ?如果没有重定向输出你没有得到异常意味着你的用户不能创建管道,所以你必须把这个权限给用户。

    【讨论】:

    • 如果我将 RedirectStandardError 和 RedirectStandardOutput 设置为 false 一切正常。如何授予用户创建管道的权利?
    • 尝试以管理员身份运行该程序,看看会发生什么。
    • 我遇到了同样的问题。转到我试图生成的 EXE 所在的目录,并将我正在运行的帐户添加到具有读取/执行的安全选项卡中,问题就消失了。
    【解决方案3】:

    这应该有完整的文件路径和文件名,试图启动一个文件夹会导致这个错误。

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

    应用程序是否还具有管理权限?

    编辑:如果是批处理文件,需要有“batch.bat”等扩展名.bat才能正常运行。另外如果是批处理文件,不能为空,否则会抛出异常。

    【讨论】:

    • 这不是真的 - 批处理文件可以具有扩展名“cmd”并且仍然可以正常运行。有轻微差异,但不是很大
    猜你喜欢
    • 2013-05-24
    • 2015-01-02
    • 1970-01-01
    • 2022-08-19
    • 1970-01-01
    • 2014-09-14
    • 1970-01-01
    • 1970-01-01
    • 2013-02-06
    相关资源
    最近更新 更多