|
| 1 | +using Microsoft.Win32; |
| 2 | +using System; |
| 3 | + |
| 4 | +namespace ITHit.FileSystem.Samples.Common.Windows.ShellExtension |
| 5 | +{ |
| 6 | + public class ShellExtensionRegistrar |
| 7 | + { |
| 8 | + private static readonly string ClsidKeyPathFormat = @"SOFTWARE\Classes\CLSID\{0:B}"; |
| 9 | + private static readonly string LocalServer32PathFormat = @$"{ClsidKeyPathFormat}\LocalServer32"; |
| 10 | + private static readonly string SyncRootPathFormat = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\SyncRootManager\{0}"; |
| 11 | + |
| 12 | + /// <summary> |
| 13 | + /// Register shell service provider COM class and register it for sync root. |
| 14 | + /// </summary> |
| 15 | + /// <param name="syncRootId">Sync root identifier</param> |
| 16 | + /// <param name="handlerName">Name of shell service provider</param> |
| 17 | + /// <param name="handlerGuid">CLSID of shell service provider</param> |
| 18 | + /// <param name="comServerPath">Absolute path to COM server executable</param> |
| 19 | + public static void Register(string syncRootId, string handlerName, Guid handlerGuid, string comServerPath) |
| 20 | + { |
| 21 | + string handlerPath = handlerGuid.ToString("B").ToUpper(); |
| 22 | + string syncRootPath = string.Format(SyncRootPathFormat, syncRootId); |
| 23 | + |
| 24 | + using RegistryKey syncRootKey = Registry.LocalMachine.OpenSubKey(syncRootPath, true); |
| 25 | + syncRootKey.SetValue(handlerName, handlerPath); |
| 26 | + |
| 27 | + string localServer32Path = string.Format(LocalServer32PathFormat, handlerPath); |
| 28 | + using RegistryKey localServer32Key = Registry.CurrentUser.CreateSubKey(localServer32Path); |
| 29 | + localServer32Key.SetValue(null, comServerPath); |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Unregister shell service provider COM class. |
| 34 | + /// </summary> |
| 35 | + /// <param name="handlerClsid"></param> |
| 36 | + public static void Unregister(Guid handlerClsid) |
| 37 | + { |
| 38 | + string thumbnailProviderGuid = handlerClsid.ToString("B").ToUpper(); |
| 39 | + string clsidKeyPath = string.Format(ClsidKeyPathFormat, thumbnailProviderGuid); |
| 40 | + Registry.CurrentUser.DeleteSubKeyTree(clsidKeyPath, false); |
| 41 | + } |
| 42 | + } |
| 43 | +} |
0 commit comments