开发者

Ask Windows 7 - what program opens this file by default

开发者 https://www.devze.com 2023-02-05 20:01 出处:网络
开发者_JAVA百科Is jumping around the registry the best way to ask what program path opens this file extension (.avi) on Windows 7? or is there a better API to use?

开发者_JAVA百科Is jumping around the registry the best way to ask what program path opens this file extension (.avi) on Windows 7? or is there a better API to use?

What is the proper way to navigate this in the registry? I noticed when I installed the DivX player that it stole the .avi extension from VLC player. I right moused ontop of the file and set the default program to VLC, but I don't see where that gets stored in HKCU.

My end goal is to have a program that is aware of the application that a file extension is associated with. And I would like to have it ask the OS instead of storing my own independent lookup data.


You don't say what language you are developing in, however you should be able to do this with a call to assocquerystring on shlwapi.dll.

The assocquerystring API function will return file association data without having to manually dive into the registry and deal with the demons that lie within. Most languages will support calling the Windows API so you should be good to go.

More information can be found here:

http://www.pinvoke.net/default.aspx/shlwapi.assocquerystring

and here:

http://msdn.microsoft.com/en-us/library/bb773471%28VS.85%29.aspx

EDIT : Some sample code:

private void SomeProcessInYourApp()
{
    // Get association for doc/avi
    string docAsscData = AssociationsHelper.GetAssociation(".doc"); // returns : docAsscData = "C:\\Program Files\\Microsoft Office\\Office12\\WINWORD.EXE"
    string aviAsscData = AssociationsHelper.GetAssociation(".avi"); // returns : aviAsscData = "C:\\Program Files\\Windows Media Player\\wmplayer.exe"

    // Get association for an unassociated extension
    string someAsscData = AssociationsHelper.GetAssociation(".blahdeblahblahblah"); // returns : someAsscData = "C:\\Windows\\system32\\shell32.dll"
}

internal static class AssociationsHelper
{
    [DllImport("Shlwapi.dll", SetLastError = true, CharSet = CharSet.Auto)]
    static extern uint AssocQueryString(AssocF flags, AssocStr str, string pszAssoc, string pszExtra,
        [Out] StringBuilder pszOut, [In][Out] ref uint pcchOut);

    [Flags]
    enum AssocF
    {
        Init_NoRemapCLSID = 0x1,
        Init_ByExeName = 0x2,
        Open_ByExeName = 0x2,
        Init_DefaultToStar = 0x4,
        Init_DefaultToFolder = 0x8,
        NoUserSettings = 0x10,
        NoTruncate = 0x20,
        Verify = 0x40,
        RemapRunDll = 0x80,
        NoFixUps = 0x100,
        IgnoreBaseClass = 0x200
    }

    enum AssocStr
    {
        Command = 1,
        Executable,
        FriendlyDocName,
        FriendlyAppName,
        NoOpen,
        ShellNewValue,
        DDECommand,
        DDEIfExec,
        DDEApplication,
        DDETopic
    }

    public static string GetAssociation(string doctype)
    {
        uint pcchOut = 0;   // size of output buffer

        // First call is to get the required size of output buffer
        AssocQueryString(AssocF.Verify, AssocStr.Executable, doctype, null, null, ref pcchOut);

        // Allocate the output buffer
        StringBuilder pszOut = new StringBuilder((int)pcchOut);

        // Get the full pathname to the program in pszOut
        AssocQueryString(AssocF.Verify, AssocStr.Executable, doctype, null, pszOut, ref pcchOut);

        string doc = pszOut.ToString();
        return doc;
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消