再谈利用SharpShell实现Windows Shell扩展
来源:互联网 发布:宜家 知乎 编辑:程序博客网 时间:2024/06/08 09:57
之前写过一篇文章介绍过如何用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{ /// <summary> /// The IconPreviewHandler is a preview handler that shows the icons contained /// in an ico file. /// </summary> [ComVisible(true)] [COMServerAssociation(AssociationType.ClassOfExtension, ".example")] [Guid("DE6D955A-BA3A-4CE6-962F-85BCFB8BF37F")] [PreviewHandler] public class HidasPreview : SharpPreviewHandler { /// <summary> /// DoPreview must create the preview handler user interface and initialize it with data /// provided by the shell. /// </summary> /// <returns> /// The preview handler user interface. /// </returns> protected override PreviewHandlerControl DoPreview() { // Create the handler control. var handler = new HidasPreviewHandlerControl(); // Do we have a file path? If so, we can do a preview. if (!string.IsNullOrEmpty(SelectedFilePath)) handler.DoPreview(SelectedFilePath); // Return the handler control. return handler; } }}
可以看到,DoPreview程序主要作用是给资源管理器返回一个PreviewHandlerControl(继承于UserControl)
然后我们可以在程序入口中加入PreviewHandler的注册和删除方法
using System;using System.Collections.Generic;using System.Linq;using System.Text;using HidasPreview;using HidasIconHandler;namespace HidasRegistry{ class Program { static void Main(string[] args) { HidasPreview.HidasPreview preview = new HidasPreview.HidasPreview(); HidasIconHandler.HidasIconHandler icon = new HidasIconHandler.HidasIconHandler(); if (args.Length == 1 && args[0] == "-a") { SharpShell.ServerRegistration.ServerRegistrationManager.InstallServer(preview, SharpShell.ServerRegistration.RegistrationType.OS32Bit, true); SharpShell.ServerRegistration.ServerRegistrationManager.InstallServer(preview, SharpShell.ServerRegistration.RegistrationType.OS64Bit, true); SharpShell.ServerRegistration.ServerRegistrationManager.RegisterServer(preview, SharpShell.ServerRegistration.RegistrationType.OS32Bit); SharpShell.ServerRegistration.ServerRegistrationManager.RegisterServer(preview, SharpShell.ServerRegistration.RegistrationType.OS64Bit); SharpShell.ServerRegistration.ServerRegistrationManager.InstallServer(icon, SharpShell.ServerRegistration.RegistrationType.OS32Bit, true); SharpShell.ServerRegistration.ServerRegistrationManager.InstallServer(icon, SharpShell.ServerRegistration.RegistrationType.OS64Bit, true); SharpShell.ServerRegistration.ServerRegistrationManager.RegisterServer(icon, SharpShell.ServerRegistration.RegistrationType.OS32Bit); SharpShell.ServerRegistration.ServerRegistrationManager.RegisterServer(icon, SharpShell.ServerRegistration.RegistrationType.OS64Bit); } if (args.Length == 1 && args[0] == "-d") { SharpShell.ServerRegistration.ServerRegistrationManager.UnregisterServer(preview, SharpShell.ServerRegistration.RegistrationType.OS32Bit); SharpShell.ServerRegistration.ServerRegistrationManager.UninstallServer(preview, SharpShell.ServerRegistration.RegistrationType.OS32Bit); SharpShell.ServerRegistration.ServerRegistrationManager.UnregisterServer(preview, SharpShell.ServerRegistration.RegistrationType.OS64Bit); SharpShell.ServerRegistration.ServerRegistrationManager.UninstallServer(preview, SharpShell.ServerRegistration.RegistrationType.OS64Bit); SharpShell.ServerRegistration.ServerRegistrationManager.UnregisterServer(icon, SharpShell.ServerRegistration.RegistrationType.OS32Bit); SharpShell.ServerRegistration.ServerRegistrationManager.UninstallServer(icon, SharpShell.ServerRegistration.RegistrationType.OS32Bit); SharpShell.ServerRegistration.ServerRegistrationManager.UnregisterServer(icon, SharpShell.ServerRegistration.RegistrationType.OS64Bit); SharpShell.ServerRegistration.ServerRegistrationManager.UninstallServer(icon, SharpShell.ServerRegistration.RegistrationType.OS64Bit); Console.WriteLine("deleted"); } Console.WriteLine(args.Length); foreach(var v in args) Console.WriteLine(v); } }}
ServerRegistration**
可以帮我们完成上述的步骤2和3,至于步骤1,我们还是需要写一个注册表项让自定义文件和后缀名关联起来。
**
欢迎各位朋友提出宝贵意见
QQ:2961688520
- 再谈利用SharpShell实现Windows Shell扩展
- Delphi实现shell扩展
- Delphi实现shell扩展
- Windows Shell编程-第十五章.SHELL扩展
- Windows Shell 扩展编程 第十五章
- Windows Shell 扩展编程 第十五章
- Windows 7下使用注册表扩展Shell
- Windows shell 扩展编程教程详解
- 利用Delphi编写Windows外壳扩展
- 利用Windows外壳扩展保护文件夹
- 利用Windows外壳扩展保护文件夹
- 利用Windows外壳扩展保护文件夹
- 利用Windows外壳扩展保护文件夹
- 利用Windows外壳扩展保护文件夹
- Windows Shell扩展系列文章 1 - .NET 4 编写Windows Shell上下文菜单扩展
- Windows Shell扩展系列文章 1 - .NET 4 编写Windows Shell上下文菜单扩展
- 利用Shell对象控制Windows系统
- VC下轻松实现扩展shell(一)
- PostgreSQL配置优化
- 关于类名后加&符号时基类引用派生类的问题
- iOS开发之集成百度地图
- delphi数据库数据显示
- Docker详解
- 再谈利用SharpShell实现Windows Shell扩展
- Web性能优化之Apache篇
- system()函数用法
- iOS UI篇5—核心动画简介
- 蓝桥杯 最大最小公倍数(java题解)
- iOS应用运用设计模式中的Strategy策略模式的开发实例
- 好用的vpn推荐
- UVa 10304 - Optimal Binary Search Tree
- Cent os ssh启动问题