c# - 启动进程时出现 System.ComponentModel.Win32Exception - 找不到文件,但文件存在

标签 c# exception file-access file-not-found win32exception

我正在尝试为我的自动启动创建一个管理器。它应该读取一个 XML 文件,然后以自定义延迟启动我的程序。例如:

<startup id="0">
    <name>Realtek Audio Manager</name>
    <process arguments="-s">C:\Program Files\Realtek\Audio\HDA\RtkNGUI64.exe</process>
    <delay>5</delay>
</startup>

这会在 5 秒后运行指定的进程 (C:\Program Files\...\RtkNGUI64.exe -s)。

现在,其中三个程序无法启动,给我一个 System.ComponentModel.Win32Exception:“Das System kann die angegebene Datei nicht finden。” (“系统无法找到指定的文件。”)

但是XML解析正确,我要启动的文件在我在XML文件中指定的位置。

问题只涉及这三个文件:
Intel HotkeysCmd - C:\Windows\System32\hkcmd.exe
英特尔 GFX 托盘 - C:\Windows\System32\igfxtray.exe
英特尔持久性 - C:\Windows\System32\igfxpers.exe

我认为问题出在文件的位置:它们都位于 C:\Windows\System32 中,所有其他工作程序都位于外部(C:\Program Files、C:\Program Files (x86), D:\Program Files, %AppData%)

我是否必须为我的程序提供某种访问权限才能启动 C:\Windows\System32 中的程序?我该怎么做?

如果不是,我在使用这些程序时出错的原因可能是什么?

编辑 - 我的代码:

delegate(object o)
{
    var s = (Startup) o;
    var p = new System.Diagnostics.Process
                {
                    StartInfo =
                        new System.Diagnostics.ProcessStartInfo(s.Process, s.Arguments)
                };
    try
    {
        s.Process = @"C:\Windows\System32\igfxtray.exe"; // For debugging purposes
        System.Diagnostics.Process.Start(s.Process);
        icon.ShowBalloonTip(2000, "StartupManager",
                            "\"" + s.Name + "\" has been started.",
                            System.Windows.Forms.ToolTipIcon.Info);
    }
    catch (System.ComponentModel.Win32Exception)
    {
        icon.ShowBalloonTip(2000, "StartupManager",
                            "\"" + s.Name + "\" could not be found.",
                            System.Windows.Forms.ToolTipIcon.Error);
    }
}

最佳答案

显然您使用的是 64 位版本的 Windows。 c:\windows\system32 和 c:\program files 目录受称为“文件系统重定向”的功能的约束。这是一个 appcompat 功能,它有助于确保 32 位进程不会尝试使用 64 位可执行文件。它们将被重定向到 c:\windows\syswow64 和 c:\program files (x86)。

因此,当您尝试启动 c:\program files\realtek\etcetera 中的文件时,您的 32 位程序将被重定向到 c:\program files (x86)\realtek\etcetera。一个不存在的目录,kaboom。 igfxtray.exe 的成分相同

您需要更改程序的平台目标,以便它可以作为 native 64 位进程运行并避免您现在遇到的重定向问题。 Project + Properties,Build 选项卡,将“Platform target”设置更改为 AnyCPU。

关于c# - 启动进程时出现 System.ComponentModel.Win32Exception - 找不到文件,但文件存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13974653/

相关文章:

java - Hibernate JPA 持久性异常

javascript - 使用 JavaScript 访问本地文件

osgi - 如何访问 OSGi 包中的文件?

c# - 如何将随机生成的 ID 显示到多个文本框中

c# - WebClient 禁止打开维基百科页面?

c# - 运行 epstopdf 时出现问题。检查你的 TeX 安装

exception - Scala:无法捕获闭包内抛出的异常

php - 如何获取错误代码的文本?

c# - Web API Restful 服务 ReadAsAsync<type> 不返回列表