询问 Windows 7 - 默认情况下什么程序打开此文件

发布于 2024-10-13 04:53:24 字数 257 浏览 5 评论 0原文

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

在注册表中导航此内容的正确方法是什么?我注意到,当我安装 DivX 播放器时,它从 VLC 播放器中窃取了 .avi 扩展名。我在文件顶部右键单击并将默认程序设置为 VLC,但我没有看到它存储在 HKCU 中的位置。

我的最终目标是拥有一个能够识别与文件扩展名关联的应用程序的程序。我想让它询问操作系统,而不是存储我自己的独立查找数据。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

老娘不死你永远是小三 2024-10-20 04:53:24

您没有说明您正在使用哪种语言进行开发,但是您应该能够通过调用 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;
    }
}

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文