去掉没必要的GuidInfo.TryGetGuid方法,替换为GuidEx.TreParse

This commit is contained in:
蓝点lilac
2020-12-29 11:52:09 +08:00
parent 207a7a4dd9
commit 7d625f4166
7 changed files with 70 additions and 64 deletions

View File

@@ -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,

View File

@@ -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();
}

View File

@@ -13,10 +13,10 @@ namespace ContextMenuManager.Controls
public void LoadItems()
{
this.AddNewItem();
this.LoadCommonItems();
this.LoadBlockedItems();
}
private void LoadCommonItems()
private void LoadBlockedItems()
{
List<string> values = new List<string>();
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);
}
};
}

View File

@@ -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
{

View File

@@ -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;
}
}

View File

@@ -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)
{

View File

@@ -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<Guid, IconLocation> IconLocationDic = new Dictionary<Guid, IconLocation>();
private static readonly Dictionary<Guid, string> FilePathDic = new Dictionary<Guid, string>();
public static readonly Dictionary<Guid, string> ItemTextDic = new Dictionary<Guid, string>();
public static readonly Dictionary<Guid, Image> ItemImageDic = new Dictionary<Guid, Image>();
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;