From 6b5d644bd9b299a1d382482d6ebbf1dc374d2ce2 Mon Sep 17 00:00:00 2001 From: Sunny Date: Fri, 27 Dec 2024 21:28:27 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- SunnyUI/Common/UApplicationEx.cs | 115 ++++++++++++------------ SunnyUI/Controls/UIColorPicker.cs | 2 +- SunnyUI/Controls/UIComboBox.cs | 2 +- SunnyUI/Controls/UIComboDataGridView.cs | 2 +- SunnyUI/Controls/UIComboTreeView.cs | 2 +- SunnyUI/Controls/UIControl.cs | 2 +- SunnyUI/Controls/UIDoubleUpDown.cs | 2 +- SunnyUI/Controls/UIFlowLayoutPanel.cs | 2 +- SunnyUI/Controls/UIIPTextBox.cs | 2 +- SunnyUI/Controls/UIImageListBox.cs | 2 +- SunnyUI/Controls/UIIntegerUpDown.cs | 2 +- SunnyUI/Controls/UIListBox.cs | 2 +- SunnyUI/Controls/UINumPadTextBox.cs | 2 +- SunnyUI/Controls/UIRichTextBox.cs | 2 +- SunnyUI/Controls/UITextBox.cs | 2 +- SunnyUI/Controls/UIUserControl.cs | 2 +- 16 files changed, 73 insertions(+), 72 deletions(-) diff --git a/SunnyUI/Common/UApplicationEx.cs b/SunnyUI/Common/UApplicationEx.cs index 203f310f..413c795c 100644 --- a/SunnyUI/Common/UApplicationEx.cs +++ b/SunnyUI/Common/UApplicationEx.cs @@ -1,67 +1,77 @@ -using Microsoft.Win32; -using System; +using System; using System.Linq; using System.Reflection; using System.Runtime.InteropServices; using System.Windows.Forms; +using Microsoft.Win32; namespace Sunny.UI { public static class ApplicationEx { - private static string StartUpPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; + private static readonly string StartUpPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; - //获取GUID + /// + /// 获取当前应用程序的 GUID + /// + /// 应用程序的 GUID public static Guid AppGuid() { - Assembly asm = Assembly.GetEntryAssembly(); + var asm = Assembly.GetEntryAssembly(); + if (asm == null) return Guid.Empty; object[] attr = (asm.GetCustomAttributes(typeof(GuidAttribute), true)); - return new Guid((attr[0] as GuidAttribute).Value); + return attr.Length > 0 && attr[0] is GuidAttribute ga ? new Guid(ga.Value) : Guid.Empty; } - public static string Folder(this Environment.SpecialFolder specialFolder) - { - return Environment.GetFolderPath(specialFolder).DealPath(); - } + /// + /// 获取指定特殊文件夹的路径,并处理路径格式 + /// + /// 特殊文件夹枚举 + /// 处理后的文件夹路径 + public static string Folder(this Environment.SpecialFolder specialFolder) => Environment.GetFolderPath(specialFolder).DealPath(); + /// + /// 获取指定特殊文件夹下的应用程序文件夹路径,并在必要时创建该文件夹 + /// + /// 特殊文件夹枚举 + /// 如果文件夹不存在,是否创建 + /// 应用程序文件夹路径 public static string FolderWithApplication(this Environment.SpecialFolder specialFolder, bool createIfNotExists = true) { - string dir = (specialFolder.Folder() + Application.ProductName).DealPath(); + var dir = (specialFolder.Folder() + Application.ProductName).DealPath(); if (createIfNotExists) Dir.CreateDir(dir); return dir; } /// - /// 用作当前漫游用户的应用程序特定数据的公共储存库的目录。 漫游用户在网络上的多台计算机上工作。 漫游用户的配置文件保留在网络服务器上,并在用户登录时加载到系统中。 + /// 获取当前漫游用户的应用程序特定数据的公共储存库的目录 /// C:\Users\{YourUserName}\AppData\Roaming\{Application.ProductName}\ /// - /// + /// 应用程序数据文件夹路径 public static string ApplicationDataFolder() => Environment.SpecialFolder.ApplicationData.FolderWithApplication(); /// - /// 用作当前非漫游用户使用的应用程序特定数据的公共储存库的目录。 + /// 获取当前非漫游用户使用的应用程序特定数据的公共储存库的目录 /// C:\Users\{YourUserName}\AppData\Local\{Application.ProductName}\ /// - /// + /// 本地应用程序数据文件夹路径 public static string LocalApplicationDataFolder() => Environment.SpecialFolder.LocalApplicationData.FolderWithApplication(); /// - /// 用作所有用户使用的应用程序特定数据的公共储存库的目录。 + /// 获取所有用户使用的应用程序特定数据的公共储存库的目录 /// C:\ProgramData \{Application.ProductName}\ /// - /// + /// 公共应用程序数据文件夹路径 public static string CommonApplicationDataFolder() => Environment.SpecialFolder.CommonApplicationData.FolderWithApplication(); /// /// 增加当前程序到开机自动运行 /// - /// + /// 启动参数 public static void AddToStartup(string arguments) { - using (RegistryKey key = Registry.CurrentUser.OpenSubKey(StartUpPath, true)) - { - key.SetValue(Application.ProductName, "\"" + Application.ExecutablePath + "\" " + arguments); - } + using var key = Registry.CurrentUser.OpenSubKey(StartUpPath, true); + key?.SetValue(Application.ProductName, $"\"{Application.ExecutablePath}\" {arguments}"); } /// @@ -69,10 +79,8 @@ namespace Sunny.UI /// public static void AddToStartup() { - using (RegistryKey key = Registry.CurrentUser.OpenSubKey(StartUpPath, true)) - { - key.SetValue(Application.ProductName, "\"" + Application.ExecutablePath + "\""); - } + using var key = Registry.CurrentUser.OpenSubKey(StartUpPath, true); + key?.SetValue(Application.ProductName, $"\"{Application.ExecutablePath}\""); } /// @@ -80,22 +88,18 @@ namespace Sunny.UI /// public static void RemoveFromStartup() { - using (RegistryKey key = Registry.CurrentUser.OpenSubKey(StartUpPath, true)) - { - key.DeleteValue(Application.ProductName, false); - } + using var key = Registry.CurrentUser.OpenSubKey(StartUpPath, true); + if (Application.ProductName != null) key?.DeleteValue(Application.ProductName, false); } /// /// 判断当前程序是否开机自动运行 /// - /// + /// 是否开机自动运行 public static bool StartupEnabled() { - using (RegistryKey key = Registry.CurrentUser.OpenSubKey(StartUpPath, false)) - { - return key.GetValue(Application.ProductName) != null; - } + using var key = Registry.CurrentUser.OpenSubKey(StartUpPath, false); + return key != null && key.GetValue(Application.ProductName) != null; } /// @@ -103,30 +107,27 @@ namespace Sunny.UI /// public static void CheckAndUpdateStartupPath() { - if (StartupEnabled()) + if (!StartupEnabled()) return; + + string arg = string.Empty; + // 从注册表键值中读取启动参数 + using var key = Registry.CurrentUser.OpenSubKey(StartUpPath, true); + if (key == null) return; + string oldValue = key.GetValue(Application.ProductName)?.ToString(); + if (oldValue == null) return; + if (oldValue.StartsWith("\"")) { - string oldValue; - string arg = string.Empty; - - //Read Argument From Registry Key Value - using (RegistryKey key = Registry.CurrentUser.OpenSubKey(StartUpPath, true)) - { - oldValue = key.GetValue(Application.ProductName).ToString(); - if (oldValue.StartsWith("\"")) - { - arg = string.Join("\"", oldValue.Split('\"').Skip(2)).Trim(); - } - else if (oldValue.Contains(" ")) - { - arg = string.Join(" ", oldValue.Split(' ').Skip(1)); - } - } - - if (string.IsNullOrEmpty(arg)) - AddToStartup(); - else - AddToStartup(arg); + arg = string.Join("\"", oldValue.Split('\"').Skip(2)).Trim(); } + else if (oldValue.Contains(" ")) + { + arg = string.Join(" ", oldValue.Split(' ').Skip(1)); + } + + if (string.IsNullOrEmpty(arg)) + AddToStartup(); + else + AddToStartup(arg); } } } diff --git a/SunnyUI/Controls/UIColorPicker.cs b/SunnyUI/Controls/UIColorPicker.cs index 634790b7..0505542b 100644 --- a/SunnyUI/Controls/UIColorPicker.cs +++ b/SunnyUI/Controls/UIColorPicker.cs @@ -77,7 +77,7 @@ namespace Sunny.UI } [Browsable(false)] - public override string[] FormTranslatorProperties { get; } + public override string[] FormTranslatorProperties => null; /// /// 颜色改变事件 diff --git a/SunnyUI/Controls/UIComboBox.cs b/SunnyUI/Controls/UIComboBox.cs index 9f450f69..9492338c 100644 --- a/SunnyUI/Controls/UIComboBox.cs +++ b/SunnyUI/Controls/UIComboBox.cs @@ -100,7 +100,7 @@ namespace Sunny.UI public event EventHandler SelectionChangeCommitted; [Browsable(false)] - public override string[] FormTranslatorProperties { get; } + public override string[] FormTranslatorProperties => null; [DefaultValue(0), Category("SunnyUI"), Description("垂直滚动条宽度,最小为原生滚动条宽度")] public int ScrollBarWidth diff --git a/SunnyUI/Controls/UIComboDataGridView.cs b/SunnyUI/Controls/UIComboDataGridView.cs index 387103ab..77a64d91 100644 --- a/SunnyUI/Controls/UIComboDataGridView.cs +++ b/SunnyUI/Controls/UIComboDataGridView.cs @@ -82,7 +82,7 @@ namespace Sunny.UI } [Browsable(false)] - public override string[] FormTranslatorProperties { get; } + public override string[] FormTranslatorProperties => null; [DefaultValue(true), Description("过滤框输入逐一过滤"), Category("SunnyUI")] public bool Filter1by1 { get; set; } = true; diff --git a/SunnyUI/Controls/UIComboTreeView.cs b/SunnyUI/Controls/UIComboTreeView.cs index e4f8348c..44bd89de 100644 --- a/SunnyUI/Controls/UIComboTreeView.cs +++ b/SunnyUI/Controls/UIComboTreeView.cs @@ -86,7 +86,7 @@ namespace Sunny.UI } [Browsable(false)] - public override string[] FormTranslatorProperties { get; } + public override string[] FormTranslatorProperties => null; [DefaultValue(false)] [Description("显示清除按钮"), Category("SunnyUI")] diff --git a/SunnyUI/Controls/UIControl.cs b/SunnyUI/Controls/UIControl.cs index c295585e..b1fcffb2 100644 --- a/SunnyUI/Controls/UIControl.cs +++ b/SunnyUI/Controls/UIControl.cs @@ -56,7 +56,7 @@ namespace Sunny.UI [Browsable(false)] [Description("控件在界面显示时需要多语翻译的属性名称数组"), Category("SunnyUI")] - public virtual string[] FormTranslatorProperties { get; } + public virtual string[] FormTranslatorProperties => null; [DefaultValue(true)] [Description("控件在界面显示时需要多语翻译"), Category("SunnyUI")] diff --git a/SunnyUI/Controls/UIDoubleUpDown.cs b/SunnyUI/Controls/UIDoubleUpDown.cs index 8f7c8a27..6d566495 100644 --- a/SunnyUI/Controls/UIDoubleUpDown.cs +++ b/SunnyUI/Controls/UIDoubleUpDown.cs @@ -66,7 +66,7 @@ namespace Sunny.UI } [Browsable(false)] - public override string[] FormTranslatorProperties { get; } + public override string[] FormTranslatorProperties => null; /// /// 需要额外设置ToolTip的控件 diff --git a/SunnyUI/Controls/UIFlowLayoutPanel.cs b/SunnyUI/Controls/UIFlowLayoutPanel.cs index 4d6dd09a..71e89aae 100644 --- a/SunnyUI/Controls/UIFlowLayoutPanel.cs +++ b/SunnyUI/Controls/UIFlowLayoutPanel.cs @@ -74,7 +74,7 @@ namespace Sunny.UI } [Browsable(false)] - public override string[] FormTranslatorProperties { get; } + public override string[] FormTranslatorProperties => null; public void Render() { diff --git a/SunnyUI/Controls/UIIPTextBox.cs b/SunnyUI/Controls/UIIPTextBox.cs index 1642c9ec..1e6ccc75 100644 --- a/SunnyUI/Controls/UIIPTextBox.cs +++ b/SunnyUI/Controls/UIIPTextBox.cs @@ -63,7 +63,7 @@ namespace Sunny.UI } [Browsable(false)] - public override string[] FormTranslatorProperties { get; } + public override string[] FormTranslatorProperties => null; public event EventHandler ValueChanged; diff --git a/SunnyUI/Controls/UIImageListBox.cs b/SunnyUI/Controls/UIImageListBox.cs index 15161d5e..c20d3a51 100644 --- a/SunnyUI/Controls/UIImageListBox.cs +++ b/SunnyUI/Controls/UIImageListBox.cs @@ -80,7 +80,7 @@ namespace Sunny.UI } [Browsable(false)] - public override string[] FormTranslatorProperties { get; } + public override string[] FormTranslatorProperties => null; public override void SetDPIScale() { diff --git a/SunnyUI/Controls/UIIntegerUpDown.cs b/SunnyUI/Controls/UIIntegerUpDown.cs index eefbd26e..59494845 100644 --- a/SunnyUI/Controls/UIIntegerUpDown.cs +++ b/SunnyUI/Controls/UIIntegerUpDown.cs @@ -63,7 +63,7 @@ namespace Sunny.UI } [Browsable(false)] - public override string[] FormTranslatorProperties { get; } + public override string[] FormTranslatorProperties => null; /// /// 需要额外设置ToolTip的控件 diff --git a/SunnyUI/Controls/UIListBox.cs b/SunnyUI/Controls/UIListBox.cs index f0e062cd..774a2437 100644 --- a/SunnyUI/Controls/UIListBox.cs +++ b/SunnyUI/Controls/UIListBox.cs @@ -98,7 +98,7 @@ namespace Sunny.UI } [Browsable(false)] - public override string[] FormTranslatorProperties { get; } + public override string[] FormTranslatorProperties => null; public override void SetDPIScale() { diff --git a/SunnyUI/Controls/UINumPadTextBox.cs b/SunnyUI/Controls/UINumPadTextBox.cs index a748dce8..e6f83cc4 100644 --- a/SunnyUI/Controls/UINumPadTextBox.cs +++ b/SunnyUI/Controls/UINumPadTextBox.cs @@ -45,7 +45,7 @@ namespace Sunny.UI } [Browsable(false)] - public override string[] FormTranslatorProperties { get; } + public override string[] FormTranslatorProperties => null; public delegate void OnValueChanged(object sender, string value); public event OnValueChanged ValueChanged; diff --git a/SunnyUI/Controls/UIRichTextBox.cs b/SunnyUI/Controls/UIRichTextBox.cs index 00c63ca0..61c1d06e 100644 --- a/SunnyUI/Controls/UIRichTextBox.cs +++ b/SunnyUI/Controls/UIRichTextBox.cs @@ -87,7 +87,7 @@ namespace Sunny.UI } [Browsable(false)] - public override string[] FormTranslatorProperties { get; } + public override string[] FormTranslatorProperties => null; protected override void Dispose(bool disposing) { diff --git a/SunnyUI/Controls/UITextBox.cs b/SunnyUI/Controls/UITextBox.cs index 69f2d099..18a4701c 100644 --- a/SunnyUI/Controls/UITextBox.cs +++ b/SunnyUI/Controls/UITextBox.cs @@ -152,7 +152,7 @@ namespace Sunny.UI } [Browsable(false)] - public override string[] FormTranslatorProperties { get; } + public override string[] FormTranslatorProperties => null; private void Edit_FontChanged(object sender, EventArgs e) { diff --git a/SunnyUI/Controls/UIUserControl.cs b/SunnyUI/Controls/UIUserControl.cs index dd453659..e2afb243 100644 --- a/SunnyUI/Controls/UIUserControl.cs +++ b/SunnyUI/Controls/UIUserControl.cs @@ -56,7 +56,7 @@ namespace Sunny.UI [Browsable(false)] [Description("控件在界面显示时需要多语翻译的属性名称数组"), Category("SunnyUI")] - public virtual string[] FormTranslatorProperties { get; } + public virtual string[] FormTranslatorProperties => null; [DefaultValue(true)] [Description("控件在界面显示时需要多语翻译"), Category("SunnyUI")]