ShellExItem.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using BluePointLilac.Controls;
  2. using BluePointLilac.Methods;
  3. using ContextMenuManager.Controls.Interfaces;
  4. using Microsoft.Win32;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Windows.Forms;
  8. namespace ContextMenuManager.Controls
  9. {
  10. sealed class ShellExItem : MyListItem, IChkVisibleItem, IBtnShowMenuItem, IFoldSubItem, ITsiGuidItem,
  11. ITsiWebSearchItem, ITsiFilePathItem, ITsiRegPathItem, ITsiRegDeleteItem, ITsiRegExportItem
  12. {
  13. public static Dictionary<string, Guid> GetPathAndGuids(string shellExPath, bool isDragDrop = false)
  14. {
  15. Dictionary<string, Guid> dic = new Dictionary<string, Guid>();
  16. string[] parts = isDragDrop ? DdhParts : CmhParts;
  17. foreach(string part in parts)
  18. {
  19. using(RegistryKey cmKey = RegistryEx.GetRegistryKey($@"{shellExPath}\{part}"))
  20. {
  21. if(cmKey == null) continue;
  22. foreach(string keyName in cmKey.GetSubKeyNames())
  23. {
  24. try
  25. {
  26. using(RegistryKey key = cmKey.OpenSubKey(keyName))
  27. {
  28. if(!GuidEx.TryParse(key.GetValue("")?.ToString(), out Guid guid))
  29. GuidEx.TryParse(keyName, out guid);
  30. if(!guid.Equals(Guid.Empty))
  31. dic.Add(key.Name, guid);
  32. }
  33. }
  34. catch { continue; }
  35. }
  36. }
  37. }
  38. return dic;
  39. }
  40. public static readonly string[] DdhParts = { "DragDropHandlers", "-DragDropHandlers" };
  41. public static readonly string[] CmhParts = { "ContextMenuHandlers", "-ContextMenuHandlers" };
  42. private const string LnkOpenGuid = "00021401-0000-0000-c000-000000000046";
  43. public ShellExItem(Guid guid, string regPath)
  44. {
  45. InitializeComponents();
  46. this.Guid = guid;
  47. this.RegPath = regPath;
  48. }
  49. private string regPath;
  50. public string RegPath
  51. {
  52. get => regPath;
  53. set
  54. {
  55. regPath = value;
  56. this.Text = this.ItemText;
  57. this.Image = GuidInfo.GetImage(Guid);
  58. ChkVisible.Checked = this.ItemVisible;
  59. }
  60. }
  61. public Guid Guid { get; set; }
  62. public string ValueName => null;
  63. public string SearchText => Text;
  64. public string ItemFilePath => GuidInfo.GetFilePath(Guid);
  65. private string KeyName => RegistryEx.GetKeyName(RegPath);
  66. private string ParentPath => RegistryEx.GetParentPath(RegPath);
  67. private string ShellExPath => RegistryEx.GetParentPath(ParentPath);
  68. private string ParentKeyName => RegistryEx.GetKeyName(ParentPath);
  69. private string DefaultValue => Registry.GetValue(RegPath, "", null)?.ToString();
  70. public string ItemText => GuidInfo.GetText(Guid) ?? (KeyName.Equals(Guid.ToString("B"), StringComparison.OrdinalIgnoreCase) ? DefaultValue : KeyName);
  71. private bool IsOpenLnkItem => Guid.ToString() == LnkOpenGuid;
  72. public bool IsDragDropItem => ParentKeyName.EndsWith(DdhParts[0], StringComparison.OrdinalIgnoreCase);
  73. private string BackupPath
  74. {
  75. get
  76. {
  77. string[] parts = IsDragDropItem ? DdhParts : CmhParts;
  78. return $@"{ShellExPath}\{(ItemVisible ? parts[1] : parts[0])}\{KeyName}";
  79. }
  80. }
  81. public bool ItemVisible
  82. {
  83. get
  84. {
  85. string[] parts = IsDragDropItem ? DdhParts : CmhParts;
  86. return ParentKeyName.Equals(parts[0], StringComparison.OrdinalIgnoreCase);
  87. }
  88. set
  89. {
  90. if(!value && TryProtectOpenItem()) return;
  91. try
  92. {
  93. RegistryEx.MoveTo(RegPath, BackupPath);
  94. }
  95. catch
  96. {
  97. MessageBoxEx.Show(AppString.Message.AuthorityProtection);
  98. return;
  99. }
  100. RegPath = BackupPath;
  101. }
  102. }
  103. public VisibleCheckBox ChkVisible { get; set; }
  104. public MenuButton BtnShowMenu { get; set; }
  105. public WebSearchMenuItem TsiSearch { get; set; }
  106. public FilePropertiesMenuItem TsiFileProperties { get; set; }
  107. public FileLocationMenuItem TsiFileLocation { get; set; }
  108. public RegLocationMenuItem TsiRegLocation { get; set; }
  109. public DeleteMeMenuItem TsiDeleteMe { get; set; }
  110. public RegExportMenuItem TsiRegExport { get; set; }
  111. public IFoldGroupItem FoldGroupItem { get; set; }
  112. public HandleGuidMenuItem TsiHandleGuid { get; set; }
  113. readonly ToolStripMenuItem TsiDetails = new ToolStripMenuItem(AppString.Menu.Details);
  114. private void InitializeComponents()
  115. {
  116. BtnShowMenu = new MenuButton(this);
  117. ChkVisible = new VisibleCheckBox(this);
  118. TsiSearch = new WebSearchMenuItem(this);
  119. TsiFileLocation = new FileLocationMenuItem(this);
  120. TsiFileProperties = new FilePropertiesMenuItem(this);
  121. TsiRegLocation = new RegLocationMenuItem(this);
  122. TsiRegExport = new RegExportMenuItem(this);
  123. TsiDeleteMe = new DeleteMeMenuItem(this);
  124. TsiHandleGuid = new HandleGuidMenuItem(this, true);
  125. ContextMenuStrip.Items.AddRange(new ToolStripItem[] { TsiHandleGuid, new ToolStripSeparator(),
  126. TsiDetails, new ToolStripSeparator(), TsiDeleteMe });
  127. TsiDetails.DropDownItems.AddRange(new ToolStripItem[] { TsiSearch, new ToolStripSeparator(),
  128. TsiFileProperties, TsiFileLocation, TsiRegLocation, TsiRegExport});
  129. ContextMenuStrip.Opening += (sender, e) => TsiDeleteMe.Enabled = !(IsOpenLnkItem && AppConfig.ProtectOpenItem);
  130. }
  131. private bool TryProtectOpenItem()
  132. {
  133. if(!IsOpenLnkItem) return false;
  134. if(!AppConfig.ProtectOpenItem) return false;
  135. return MessageBoxEx.Show(AppString.Message.PromptIsOpenItem, MessageBoxButtons.YesNo) != DialogResult.Yes;
  136. }
  137. public void DeleteMe()
  138. {
  139. try
  140. {
  141. RegistryEx.DeleteKeyTree(this.RegPath, true);
  142. RegistryEx.DeleteKeyTree(this.BackupPath);
  143. }
  144. catch
  145. {
  146. MessageBoxEx.Show(AppString.Message.AuthorityProtection);
  147. return;
  148. }
  149. this.Dispose();
  150. }
  151. }
  152. }