Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

c# - System.ComponentModel.Win32Exception when starting process - file not found, but file exists

I am trying to create a manager for my autostarts. It should read an XML file and then start my programs with a custom delay. For example:

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

This runs the specified process (C:Program Files...RtkNGUI64.exe -s) after 5 seconds.

Now, three of the programs won't start, giving me a System.ComponentModel.Win32Exception: "Das System kann die angegebene Datei nicht finden." ("The system was not able to find the specified file.")

But the XML is parsed correctly, and the file I want to start is at the location I specified in the XML file.

The problem concerns only these three files:
Intel HotkeysCmd - C:WindowsSystem32hkcmd.exe
Intel GFX Tray - C:WindowsSystem32igfxtray.exe
Intel Persistance - C:WindowsSystem32igfxpers.exe

I think that the problem comes from the location of the files: they all are located in C:WindowsSystem32, and all the other, working programs are located outside (C:Program Files, C:Program Files (x86), D:Program Files, %AppData%)

Do I have to give my program some kind of access rights to start programs in C:WindowsSystem32? How would I do that?

If not, what could be the reason I get errors with these programs?

EDIT - my code:

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:WindowsSystem32igfxtray.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);
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Clearly you are using a 64-bit version of Windows. The c:windowssystem32 and c:program files directories are subject to a feature called "file system redirection". It is an appcompat feature, it helps to ensure that 32-bit processes don't try to use 64-bit executables. They'll get redirected to c:windowssyswow64 and c:program files (x86).

So when you try to start a file in c:program files ealteketcetera, your 32-bit program will be redirected to c:program files (x86) ealteketcetera. A directory that doesn't exist, kaboom. Same ingredient for igfxtray.exe

You'll need to change your program's platform target so it can run as a native 64-bit process and avoid the redirection problem you now have. Project + Properties, Build tab, change the "Platform target" setting to AnyCPU.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...