重构
This commit is contained in:
parent
b01e4e14e1
commit
6b5d644bd9
@ -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
|
||||
/// <summary>
|
||||
/// 获取当前应用程序的 GUID
|
||||
/// </summary>
|
||||
/// <returns>应用程序的 GUID</returns>
|
||||
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();
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取指定特殊文件夹的路径,并处理路径格式
|
||||
/// </summary>
|
||||
/// <param name="specialFolder">特殊文件夹枚举</param>
|
||||
/// <returns>处理后的文件夹路径</returns>
|
||||
public static string Folder(this Environment.SpecialFolder specialFolder) => Environment.GetFolderPath(specialFolder).DealPath();
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定特殊文件夹下的应用程序文件夹路径,并在必要时创建该文件夹
|
||||
/// </summary>
|
||||
/// <param name="specialFolder">特殊文件夹枚举</param>
|
||||
/// <param name="createIfNotExists">如果文件夹不存在,是否创建</param>
|
||||
/// <returns>应用程序文件夹路径</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 用作当前漫游用户的应用程序特定数据的公共储存库的目录。 漫游用户在网络上的多台计算机上工作。 漫游用户的配置文件保留在网络服务器上,并在用户登录时加载到系统中。
|
||||
/// 获取当前漫游用户的应用程序特定数据的公共储存库的目录
|
||||
/// C:\Users\{YourUserName}\AppData\Roaming\{Application.ProductName}\
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <returns>应用程序数据文件夹路径</returns>
|
||||
public static string ApplicationDataFolder() => Environment.SpecialFolder.ApplicationData.FolderWithApplication();
|
||||
|
||||
/// <summary>
|
||||
/// 用作当前非漫游用户使用的应用程序特定数据的公共储存库的目录。
|
||||
/// 获取当前非漫游用户使用的应用程序特定数据的公共储存库的目录
|
||||
/// C:\Users\{YourUserName}\AppData\Local\{Application.ProductName}\
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <returns>本地应用程序数据文件夹路径</returns>
|
||||
public static string LocalApplicationDataFolder() => Environment.SpecialFolder.LocalApplicationData.FolderWithApplication();
|
||||
|
||||
/// <summary>
|
||||
/// 用作所有用户使用的应用程序特定数据的公共储存库的目录。
|
||||
/// 获取所有用户使用的应用程序特定数据的公共储存库的目录
|
||||
/// C:\ProgramData \{Application.ProductName}\
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <returns>公共应用程序数据文件夹路径</returns>
|
||||
public static string CommonApplicationDataFolder() => Environment.SpecialFolder.CommonApplicationData.FolderWithApplication();
|
||||
|
||||
/// <summary>
|
||||
/// 增加当前程序到开机自动运行
|
||||
/// </summary>
|
||||
/// <param name="arguments"></param>
|
||||
/// <param name="arguments">启动参数</param>
|
||||
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}");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -69,10 +79,8 @@ namespace Sunny.UI
|
||||
/// </summary>
|
||||
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}\"");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -80,22 +88,18 @@ namespace Sunny.UI
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 判断当前程序是否开机自动运行
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
/// <returns>是否开机自动运行</returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -103,30 +107,27 @@ namespace Sunny.UI
|
||||
/// </summary>
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ namespace Sunny.UI
|
||||
}
|
||||
|
||||
[Browsable(false)]
|
||||
public override string[] FormTranslatorProperties { get; }
|
||||
public override string[] FormTranslatorProperties => null;
|
||||
|
||||
/// <summary>
|
||||
/// 颜色改变事件
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
@ -86,7 +86,7 @@ namespace Sunny.UI
|
||||
}
|
||||
|
||||
[Browsable(false)]
|
||||
public override string[] FormTranslatorProperties { get; }
|
||||
public override string[] FormTranslatorProperties => null;
|
||||
|
||||
[DefaultValue(false)]
|
||||
[Description("显示清除按钮"), Category("SunnyUI")]
|
||||
|
@ -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")]
|
||||
|
@ -66,7 +66,7 @@ namespace Sunny.UI
|
||||
}
|
||||
|
||||
[Browsable(false)]
|
||||
public override string[] FormTranslatorProperties { get; }
|
||||
public override string[] FormTranslatorProperties => null;
|
||||
|
||||
/// <summary>
|
||||
/// 需要额外设置ToolTip的控件
|
||||
|
@ -74,7 +74,7 @@ namespace Sunny.UI
|
||||
}
|
||||
|
||||
[Browsable(false)]
|
||||
public override string[] FormTranslatorProperties { get; }
|
||||
public override string[] FormTranslatorProperties => null;
|
||||
|
||||
public void Render()
|
||||
{
|
||||
|
@ -63,7 +63,7 @@ namespace Sunny.UI
|
||||
}
|
||||
|
||||
[Browsable(false)]
|
||||
public override string[] FormTranslatorProperties { get; }
|
||||
public override string[] FormTranslatorProperties => null;
|
||||
|
||||
public event EventHandler ValueChanged;
|
||||
|
||||
|
@ -80,7 +80,7 @@ namespace Sunny.UI
|
||||
}
|
||||
|
||||
[Browsable(false)]
|
||||
public override string[] FormTranslatorProperties { get; }
|
||||
public override string[] FormTranslatorProperties => null;
|
||||
|
||||
public override void SetDPIScale()
|
||||
{
|
||||
|
@ -63,7 +63,7 @@ namespace Sunny.UI
|
||||
}
|
||||
|
||||
[Browsable(false)]
|
||||
public override string[] FormTranslatorProperties { get; }
|
||||
public override string[] FormTranslatorProperties => null;
|
||||
|
||||
/// <summary>
|
||||
/// 需要额外设置ToolTip的控件
|
||||
|
@ -98,7 +98,7 @@ namespace Sunny.UI
|
||||
}
|
||||
|
||||
[Browsable(false)]
|
||||
public override string[] FormTranslatorProperties { get; }
|
||||
public override string[] FormTranslatorProperties => null;
|
||||
|
||||
public override void SetDPIScale()
|
||||
{
|
||||
|
@ -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;
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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)
|
||||
{
|
||||
|
@ -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")]
|
||||
|
Loading…
x
Reference in New Issue
Block a user