locked
Getting the right Autorun path RRS feed

  • Question

  • I am trying to create an autorun utility that depends on some xml files.  Right now I am coding in C#, and PInvoking the SHGetAutoRunPath method from aygshell.dll.  The WM device I am using has multiple USB host ports.  So, if I have a USB flash drive plugged into one of the ports, it is enumerated as "Hard Disk."  When I plug in a second USB flash drive, it is named "Hard Disk 2."

    The problem is that the autorun utility on Hard Disk 2 depends on xml files that are also stored on Hard Disk 2, but when I run the SHGetAutoRunPath method, it returns "\Hard Disk\2577\Autorun.exe."  Which is the default autorun directory, not the "current" autorun directory.  This is not helpful.  I also can't use the native GetModuleFileName, or even the managed Assembly.GetExecutingAssembly, because Windows Mobile copies Autorun.exe to \Windows before running it.

    As far as I can tell, SHGetAutoRunPath is only useful to me if my autorun utility happens to be in the default autorun directory. Is there any way to know exactly which directory Autorun.exe came from before it was copied to \Windows?

    Tuesday, March 30, 2010 8:11 PM

Answers

  • I was able to find a solution to my problem.  I decided to find out exactly where Windows Mobile stores the autorun executable, and it turns out that it is stored in "\Windows\Autorun\Hard Disk" or "\Windows\Autorun\SD Card", etc. depending on where the autorun file came from.  That way, the OS knows which autorun to call when the device is removed.  So, instead of making a call to SHGetAutoRunPath I do the following:

    private const string WindowsAutorunPath = "\\Windows\\Autorun";
    private static string AutoRunPath
    {
        get
        {
            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();
            string path = System.IO.Path.GetDirectoryName(assembly.ManifestModule.FullyQualifiedName);
            return path.Substring(WindowsAutorunPath.Length);
        }
    }
    
    I'm sure that somebody who read this post over the last couple of days already knew that, but didn't want to take the time to explain it.  So, I am posting the solution here, hoping that it helps someone else.

    • Marked as answer by nispio Thursday, April 1, 2010 9:34 PM
    Thursday, April 1, 2010 9:32 PM