之前写过一篇文章介绍过如何用SharpShell提供的接口去实现Windows PreviewHandler的扩展功能,但用的方法是先实现SharpShell中的各类接口编译DLL,再手动给注册表安装和注册相关内容,但发现这种方法有点折腾,而且不便于部署,后来发现SharpShell本来就自带注册表管理器,使用起来非常方便,而且可以避免自己写注册表出现疏忽。
Windows PreviewHandler其实是一个COM组件,要实现某种特定文件格式在资源管理器中的预览,需要以下几步
1.注册你的自定义文件后缀
[HKEY_CLASSES_ROOT.example]
@=”examplefile”
2.注册你的PreviewHandler组件
[HKEY_CLASSES_ROOT\CLSID{de6d955a-ba3a-4ce6-962f-85bcfb8bf37f}]
@=”HidasPreview”
“AppID”=”{de6d955a-ba3a-4ce6-962f-85bcfb8bf380}”
“DisplayName”=”HidasPreview”
“Icon”=hex(2):25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,\
00,25,00,5c,00,73,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,66,00,\
6f,00,6e,00,74,00,65,00,78,00,74,00,2e,00,64,00,6c,00,6c,00,2c,00,31,00,30,\
00,00,00
[HKEY_CLASSES_ROOT\CLSID\{de6d955a-ba3a-4ce6-962f-85bcfb8bf37f}\InprocServer32]
@="mscoree.dll"
"Assembly"="HidasRegistry, Version=1.0.0.0, Culture=neutral, PublicKeyToken=87a8ebc60c5813a4"
"Class"="HidasPreview.HidasPreview"
"RuntimeVersion"="v4.0.30319"
"ThreadingModel"="Both"
"CodeBase"="file:///E:/VS Projects/PreviewHandlerSample/HidasRegistry/bin/Release/HidasRegistry.exe"
[HKEY_CLASSES_ROOT\CLSID\{de6d955a-ba3a-4ce6-962f-85bcfb8bf37f}\InprocServer32\1.0.0.0]
"Assembly"="HidasRegistry, Version=1.0.0.0, Culture=neutral, PublicKeyToken=87a8ebc60c5813a4"
"Class"="HidasPreview.HidasPreview"
"RuntimeVersion"="v4.0.30319"
"CodeBase"="file:///E:/VS Projects/PreviewHandlerSample/HidasRegistry/bin/Release/HidasRegistry.exe"
3.为你的自定义文件注册对应的PreviewHandler
[HKEY_CLASSES_ROOT\higetfile\shellex{8895b1c6-b41f-4c1c-a562-0d564250836f}]
@=”{de6d955a-ba3a-4ce6-962f-85bcfb8bf37f}”
可以看到,第二步中注册的程序其实是mscoree.dll,也就是.net虚拟机的初始库,而它的参数则是我们编译的SharpShell程序相关内容,包括程序地址、调用的类以及.NET版本等等。
回来再看程序代码部分,我在代码中继承实现了一个SharpPreviewHandler类
using SharpShell.Attributes;
using SharpShell.SharpPreviewHandler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace HidasPreview
{
///