From 7d625f4166580243173ebf37da3d211ad51c7169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=93=9D=E7=82=B9lilac?= Date: Tue, 29 Dec 2020 11:52:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E6=B2=A1=E5=BF=85=E8=A6=81?= =?UTF-8?q?=E7=9A=84GuidInfo.TryGetGuid=E6=96=B9=E6=B3=95=EF=BC=8C?= =?UTF-8?q?=E6=9B=BF=E6=8D=A2=E4=B8=BAGuidEx.TreParse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controls/EnhanceMenusList.cs | 2 +- .../Controls/GuidBlockedItem.cs | 46 ++++++++-------- .../Controls/GuidBlockedList.cs | 15 +++--- ContextMenuManager/Controls/ShellExItem.cs | 6 +-- ContextMenuManager/Controls/ShellItem.cs | 4 +- ContextMenuManager/Controls/ThirdRulesList.cs | 9 +++- ContextMenuManager/GuidInfo.cs | 52 +++++++++---------- 7 files changed, 70 insertions(+), 64 deletions(-) diff --git a/ContextMenuManager/Controls/EnhanceMenusList.cs b/ContextMenuManager/Controls/EnhanceMenusList.cs index 388fed2..8f36e43 100644 --- a/ContextMenuManager/Controls/EnhanceMenusList.cs +++ b/ContextMenuManager/Controls/EnhanceMenusList.cs @@ -155,7 +155,7 @@ namespace ContextMenuManager.Controls { XmlElement verXE = (XmlElement)itemXE.SelectSingleNode("OSVersion"); if(!JudgeOSVersion(verXE)) continue; - if(!GuidInfo.TryGetGuid(itemXE.GetAttribute("Guid"), out Guid guid)) continue; + if(!GuidEx.TryParse(itemXE.GetAttribute("Guid"), out Guid guid)) continue; EnhanceShellExItem item = new EnhanceShellExItem { FoldGroupItem = groupItem, diff --git a/ContextMenuManager/Controls/GuidBlockedItem.cs b/ContextMenuManager/Controls/GuidBlockedItem.cs index a2bc197..c2ab40e 100644 --- a/ContextMenuManager/Controls/GuidBlockedItem.cs +++ b/ContextMenuManager/Controls/GuidBlockedItem.cs @@ -6,41 +6,44 @@ using System.Windows.Forms; namespace ContextMenuManager.Controls { - class GuidBlockedItem : MyListItem, IBtnDeleteItem, ITsiWebSearchItem, ITsiFilePathItem, ITsiRegPathItem + class GuidBlockedItem : MyListItem, IBtnDeleteItem, ITsiWebSearchItem, ITsiFilePathItem { public const string HKLMBLOCKED = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Blocked"; public const string HKCUBLOCKED = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Shell Extensions\Blocked"; public static readonly string[] BlockedPaths = { HKLMBLOCKED, HKCUBLOCKED }; - public GuidBlockedItem(Guid guid, string guidPath) + public GuidBlockedItem(string value) { InitializeComponents(); - this.Guid = guid; - this.RegPath = guidPath; + this.Value = value; + if(GuidEx.TryParse(value, out Guid guid)) + { + this.Guid = guid; + this.Text = GuidInfo.GetText(guid); + this.Image = GuidInfo.GetImage(guid); + this.ItemFilePath = GuidInfo.GetFilePath(Guid); + } + else + { + this.Guid = Guid.Empty; + this.Text = AppString.MessageBox.MalformedGuid; + this.Image = AppImage.DllDefaultIcon; + } + this.Text += "\n" + value; } - private Guid guid; - public Guid Guid - { - get => guid; - set - { - guid = value; - this.Text = $"{GuidInfo.GetText(value)}\n{value}"; - this.Image = GuidInfo.GetImage(value); - } - } + public string Value { get; set; } + public Guid Guid { get; set; } public DeleteButton BtnDelete { get; set; } public ObjectPathButton BtnOpenPath { get; set; } - public string SearchText => Guid.ToString(); - public string ItemFilePath => GuidInfo.GetFilePath(Guid); + public string SearchText => Value; + public string ItemFilePath { get; set; } public WebSearchMenuItem TsiSearch { get; set; } public FileLocationMenuItem TsiFileLocation { get; set; } public FilePropertiesMenuItem TsiFileProperties { get; set; } - public RegLocationMenuItem TsiRegLocation { get; set; } public string RegPath { get; set; } private void InitializeComponents() @@ -50,17 +53,16 @@ namespace ContextMenuManager.Controls TsiSearch = new WebSearchMenuItem(this); TsiFileProperties = new FilePropertiesMenuItem(this); TsiFileLocation = new FileLocationMenuItem(this); - TsiRegLocation = new RegLocationMenuItem(this); - ContextMenuStrip.Items.AddRange(new ToolStripItem[] {TsiSearch, new ToolStripSeparator(), - TsiFileProperties, TsiFileLocation, TsiRegLocation }); + ContextMenuStrip.Items.AddRange(new ToolStripItem[] {TsiSearch, + new ToolStripSeparator(), TsiFileProperties, TsiFileLocation }); MyToolTip.SetToolTip(BtnDelete, AppString.Menu.Delete); } public void DeleteMe() { - Array.ForEach(BlockedPaths, path => { RegistryEx.DeleteValue(path, Guid.ToString("B")); }); + Array.ForEach(BlockedPaths, path => { RegistryEx.DeleteValue(path, this.Value); }); ExplorerRestarter.NeedRestart = true; this.Dispose(); } diff --git a/ContextMenuManager/Controls/GuidBlockedList.cs b/ContextMenuManager/Controls/GuidBlockedList.cs index 96e628b..006d881 100644 --- a/ContextMenuManager/Controls/GuidBlockedList.cs +++ b/ContextMenuManager/Controls/GuidBlockedList.cs @@ -13,10 +13,10 @@ namespace ContextMenuManager.Controls public void LoadItems() { this.AddNewItem(); - this.LoadCommonItems(); + this.LoadBlockedItems(); } - private void LoadCommonItems() + private void LoadBlockedItems() { List values = new List(); Array.ForEach(GuidBlockedItem.BlockedPaths, path => @@ -26,8 +26,7 @@ namespace ContextMenuManager.Controls }); Array.ForEach(values.Distinct(StringComparer.OrdinalIgnoreCase).ToArray(), value => { - if(GuidInfo.TryGetGuid(value, out Guid guid, out string guidPath)) - this.AddItem(new GuidBlockedItem(guid, guidPath)); + this.AddItem(new GuidBlockedItem(value)); }); } @@ -47,9 +46,9 @@ namespace ContextMenuManager.Controls { using(InputDialog dlg = new InputDialog { Title = AppString.Dialog.InputGuid }) { - if(GuidInfo.TryGetGuid(Clipboard.GetText(), out Guid guid)) dlg.Text = guid.ToString(); + if(GuidEx.TryParse(Clipboard.GetText(), out Guid guid)) dlg.Text = guid.ToString(); if(dlg.ShowDialog() != DialogResult.OK) return; - if(GuidInfo.TryGetGuid(dlg.Text, out guid, out string guidPath)) + if(GuidEx.TryParse(dlg.Text, out guid)) { Array.ForEach(GuidBlockedItem.BlockedPaths, path => { @@ -63,10 +62,10 @@ namespace ContextMenuManager.Controls return; } } - this.InsertItem(new GuidBlockedItem(guid, guidPath), 1); + this.InsertItem(new GuidBlockedItem(dlg.Text), 1); ExplorerRestarter.NeedRestart = true; } - else MessageBoxEx.Show(AppString.MessageBox.UnknownGuid); + else MessageBoxEx.Show(AppString.MessageBox.MalformedGuid); } }; } diff --git a/ContextMenuManager/Controls/ShellExItem.cs b/ContextMenuManager/Controls/ShellExItem.cs index 421fdba..58cadf7 100644 --- a/ContextMenuManager/Controls/ShellExItem.cs +++ b/ContextMenuManager/Controls/ShellExItem.cs @@ -23,8 +23,8 @@ namespace ContextMenuManager.Controls { using(RegistryKey key = cmKey.OpenSubKey(keyName)) { - if(!GuidInfo.TryGetGuid(key.GetValue("")?.ToString(), out Guid guid)) - GuidInfo.TryGetGuid(keyName, out guid); + if(!GuidEx.TryParse(key.GetValue("")?.ToString(), out Guid guid)) + GuidEx.TryParse(keyName, out guid); if(!guid.Equals(Guid.Empty)) dic.Add(key.Name, guid); } @@ -245,7 +245,7 @@ namespace ContextMenuManager.Controls try { RegistryEx.DeleteKeyTree(this.RegPath, true); - RegistryEx.DeleteKeyTree(this.BackupPath, true); + RegistryEx.DeleteKeyTree(this.BackupPath); } catch { diff --git a/ContextMenuManager/Controls/ShellItem.cs b/ContextMenuManager/Controls/ShellItem.cs index 925e8c2..7b153c6 100644 --- a/ContextMenuManager/Controls/ShellItem.cs +++ b/ContextMenuManager/Controls/ShellItem.cs @@ -355,8 +355,8 @@ namespace ContextMenuManager.Controls get { string value = Registry.GetValue(CommandPath, "DelegateExecute", null)?.ToString(); - if(GuidInfo.TryGetGuid(value, out Guid guid)) return guid; - else return Guid.Empty; + GuidEx.TryParse(value, out Guid guid); + return guid; } } diff --git a/ContextMenuManager/Controls/ThirdRulesList.cs b/ContextMenuManager/Controls/ThirdRulesList.cs index 893a789..fac5ddf 100644 --- a/ContextMenuManager/Controls/ThirdRulesList.cs +++ b/ContextMenuManager/Controls/ThirdRulesList.cs @@ -18,7 +18,14 @@ namespace ContextMenuManager.Controls foreach(XmlElement groupXE in ReadXml().DocumentElement.ChildNodes) { Guid guid = Guid.Empty; - if(groupXE.HasAttribute("Guid") && !GuidInfo.TryGetGuid(groupXE.GetAttribute("Guid"), out guid)) continue; + if(groupXE.HasAttribute("Guid")) + { + if(GuidEx.TryParse(groupXE.GetAttribute("Guid"), out guid)) + { + if(!File.Exists(GuidInfo.GetFilePath(guid))) continue; + } + else continue; + } GroupPathItem groupItem = new GroupPathItem(groupXE.GetAttribute("RegPath"), ObjectPath.PathType.Registry) { diff --git a/ContextMenuManager/GuidInfo.cs b/ContextMenuManager/GuidInfo.cs index 705c94c..e7b5d91 100644 --- a/ContextMenuManager/GuidInfo.cs +++ b/ContextMenuManager/GuidInfo.cs @@ -1,4 +1,5 @@ using BulePointLilac.Methods; +using ContextMenuManager.Controls; using Microsoft.Win32; using System; using System.Collections.Generic; @@ -23,40 +24,28 @@ namespace ContextMenuManager public int IconIndex { get; set; } } - private static IniReader AppDic; + static GuidInfo() + { + //将Skype添加到字典 + Guid skypeGuid = new Guid(RegRuleItem.SkypeGuidStr); + FilePathDic.Add(skypeGuid, null); + ItemTextDic.Add(skypeGuid, AppString.Item.ShareWithSkype); + ItemImageDic.Add(skypeGuid, AppImage.Skype); + } + + private static readonly IniReader AppDic = new IniReader(new StringBuilder(Properties.Resources.GuidInfosDic)); public static readonly IniReader UserDic = new IniReader(AppConfig.UserGuidInfosDic); + public static readonly IniReader WebDic = new IniReader(AppConfig.WebGuidInfosDic); public static readonly Dictionary IconLocationDic = new Dictionary(); private static readonly Dictionary FilePathDic = new Dictionary(); public static readonly Dictionary ItemTextDic = new Dictionary(); public static readonly Dictionary ItemImageDic = new Dictionary(); - public static bool TryGetGuid(string value, out Guid guid) => TryGetGuid(value, out guid, out _); - - public static bool TryGetGuid(string value, out Guid guid, out string guidPath) - { - guidPath = null; - if(!GuidEx.TryParse(value, out guid)) return false; - foreach(string path in ClsidPaths) - { - using(RegistryKey key = RegistryEx.GetRegistryKey($@"{path}\{guid:B}")) - { - if(key != null) - { - guidPath = key.Name; - return true; - } - } - } - return false; - } - private static bool TryGetValue(string section, string key, out string value) { //用户自定义字典优先 if(UserDic.TryGetValue(section, key, out value)) return true; - if(!File.Exists(AppConfig.WebGuidInfosDic)) - File.WriteAllText(AppConfig.WebGuidInfosDic, Properties.Resources.GuidInfosDic, Encoding.UTF8); - AppDic = AppDic ?? new IniReader(AppConfig.WebGuidInfosDic); + if(WebDic.TryGetValue(section, key, out value)) return true; if(AppDic.TryGetValue(section, key, out value)) return true; return false; } @@ -77,9 +66,18 @@ namespace ContextMenuManager { using(RegistryKey key = guidKey.OpenSubKey(keyName)) { - string value = key?.GetValue("")?.ToString(); - filePath = ObjectPath.ExtractFilePath(value); - if(File.Exists(filePath)) break; + if(key == null) continue; + string value1 = key.GetValue("CodeBase")?.ToString()?.Replace("file:///", "")?.Replace('/', '\\'); + if(File.Exists(value1)) + { + filePath = value1; break; + } + string value2 = key.GetValue("")?.ToString(); + value2 = ObjectPath.ExtractFilePath(value2); + if(File.Exists(value2)) + { + filePath = value2; break; + } } } if(File.Exists(filePath)) break;