【问题标题】:Finding preview handler GUIDs for image types (gif, bmp, jpg, etc)查找图像类型(gif、bmp、jpg 等)的预览处理程序 GUID
【发布时间】:2017-03-15 19:22:18
【问题描述】:

如果您使用 Windows 资源管理器并单击 .docx 或 .jpg 文件等项目,您将在资源管理器 like this 中预览您单击的项目。我试图在我的 Windows 窗体应用程序中复制它,它适用于 .docx 和 .xlsx 文件,但不适用于图像文件类型。 据我了解,预览处理程序在 filextension/ShellEx 中的 GUID {8895b1c6-b41f-4c1c-a562-0d564250836f} 下注册。使用 regedit 您可以看到 .docx 文件有这些 .docx previewhandler GUID,但是当您查看 .jpg 之类的文件时,什么也找不到。 (i.stack.imgur.com/40v6h.png)。 (我不能发布超过 2 个链接)

根据这篇文章的第一个答案(stackoverflow.com/questions/39373357/how-to-get-the-icon-path-and-index-associated-with-a-file-type) 预览处理程序可能会存储在其他位置以用于 .jpg,但它们对我来说都是空的。

我的问题:如何获取窗口可以找到但我找不到的扩展类型的预览处理程序。我认为在某处存储了预览处理程序,但我不知道它们在哪里或如何访问它们。

这是我用来获取文件 GUID 的代码。 .docx 和 .xlsx 类型成功,但图像类型不成功。我经过了最后一个链接中提到的每个位置,但它们都显示为空。

    private Guid GetPreviewHandlerGUID(string filename)
    {
        // open the registry key corresponding to the file extension
        string extention = Path.GetExtension(filename);
        RegistryKey ext = RegistryKey.OpenBaseKey(RegistryHive.ClassesRoot, RegistryView.Registry64);

        // open the key that indicates the GUID of the preview handler type
        string className = Convert.ToString(ext.GetValue(null));
        RegistryKey test = ext.OpenSubKey(className + "\\ShellEx\\{8895b1c6-b41f-4c1c-a562-0d564250836f}");
        if (test != null) return new Guid(Convert.ToString(test.GetValue(null)));
        // sometimes preview handlers are declared on key for the class
        if (className != null) {
                test = ext.OpenSubKey(extention + "\\ShellEx\\{8895b1c6-b41f-4c1c-a562-0d564250836f}");
            if (test == null)
                test = ext.OpenSubKey("SystemFileAssociations\\" + className + "\\ShellEx\\{8895b1c6-b41f-4c1c-a562-0d564250836f}");
            if (test == null)
                test = ext.OpenSubKey("SystemFileAssociations\\" + extention + "\\ShellEx\\{8895b1c6-b41f-4c1c-a562-0d564250836f}");
            if (test == null)
                test = ext.OpenSubKey("SystemFileAssociations\\image\\ShellEx\\{8895b1c6-b41f-4c1c-a562-0d564250836f}");
            if (test != null) return new Guid(Convert.ToString(test.GetValue(null)));
        }

        return Guid.Empty;
    }

这是我在这里的第一篇文章,所以我希望我能提供足够的信息。如果有遗漏的东西,我会在有机会时添加它们。谢谢。

【问题讨论】:

    标签: c# image guid file-extension preview-handler


    【解决方案1】:

    这是在本地机器下:

    HKEY_LOCAL_MACHINE\Software\Classes\giffile\shellex{8895b1c6-b41f-4c1c-a562-0d564250836f}

    我是通过反编译 PreviewConfig 得到的 http://www.winhelponline.com/blog/previewconfig-tool-registers-file-types-for-the-preview-pane-in-windows-vista/

    【讨论】:

      【解决方案2】:

      与其自己爬取注册表,不如使用 AssocQueryString 函数。

      您告诉它.jpg,以及您要查找的 shell 处理程序:

      • {BB2E617C-0920-11d1-9A0B-00C04FC2D6C1}:IExtractImage
      • {953BB1EE-93B4-11d1-98A3-00C04FB687DA}:IExtractImage2
      • {8895b1c6-b41f-4c1c-a562-0d564250836f}:IPreviewHandler
      • {e357fccd-a995-4576-b01f-234630154e96}:IThumbnailProvider

      它会返回那个处理程序的 clsid。

      C#风格的伪代码

      private Guid GetShellObjectClsid(String filename, Guid desiredHandler)
      {
          String ext = Path.GetExtension(filename);
          String sInterfaceID = desiredHandler.ToString("B"); // B ==> The COM format {xxxx}
      
          uint bufferLen = 100; //more than enough to hold a 38 character clsid
          StringBuilder buffer = new StringBuilder(bufferLen);
      
          HRESULT hr = AssocQueryString(ASSOCF_INIT_DEFAULTTOSTAR, ASSOCSTR_SHELLEXTENSION, 
                ext, buffer, ref bufferLen);
          if (hr != S_OK)
          {
             //Marhsal.ThrowExceptionForHR(hr);
             return null;
          }
      
          String s = sb.ToString();
      
          return new Guid(sb.ToString());   
      }
      

      所以现在如果你想要 IPreviewHandler 作为文件类型:

      Guid previewHandlerClsid = GetShellObjectClsid("a.jpg", IID_IPreviewHandler);
      

      【讨论】:

        猜你喜欢
        • 2013-05-10
        • 2012-01-24
        • 1970-01-01
        • 2010-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-10-19
        相关资源
        最近更新 更多