【问题标题】:Ask Windows 7 - what program opens this file by default询问 Windows 7 - 什么程序默认打开此文件
【发布时间】:2011-01-19 20:17:23
【问题描述】:

在注册表中跳转是询问在 Windows 7 上打开此文件扩展名 (.avi) 的程序路径的最佳方法吗?还是有更好的 API 可以使用?

在注册表中导航的正确方法是什么?当我安装 DivX 播放器时,我注意到它从 VLC 播放器中窃取了 .avi 扩展名。我在文件上用鼠标右键将默认程序设置为 VLC,但我看不到它在 HKCU 中的存储位置。

我的最终目标是让程序知道与文件扩展名关联的应用程序。而且我想让它询问操作系统,而不是存储我自己的独立查找数据。

【问题讨论】:

  • 使用什么编程语言浏览注册表?

标签: registry file-type


【解决方案1】:

你没有说你正在开发什么语言,但是你应该可以通过在shlwapi.dll上调用assocquerystring来做到这一点。

assocquerystring API 函数将返回文件关联数据,而无需手动潜入注册表并处理其中的恶魔。大多数语言都支持调用 Windows API,所以你应该很高兴。

更多信息可以在这里找到:

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

这里:

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

编辑:一些示例代码:

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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 2014-01-31
    • 1970-01-01
    相关资源
    最近更新 更多