From 1e1115d907748c3cc7c73f5b8c011341fdb4df6b Mon Sep 17 00:00:00 2001 From: AlexanderPro Date: Mon, 28 Aug 2023 16:41:46 -0700 Subject: [PATCH] Upgraded to C# 9 --- Wlx2Explorer.sln | 6 +-- Wlx2Explorer/Extensions/EnumExtensions.cs | 7 +--- Wlx2Explorer/Forms/AboutForm.cs | 6 +-- Wlx2Explorer/Forms/ProgramSettingsForm.cs | 3 +- Wlx2Explorer/Hooks/KeyboardHook.cs | 14 ++++--- Wlx2Explorer/InitializationFile.cs | 50 +++++++++++------------ Wlx2Explorer/Settings/ProgramSettings.cs | 8 ++-- Wlx2Explorer/StartUpManager.cs | 28 ++++++------- Wlx2Explorer/Utils/AssemblyUtils.cs | 2 +- Wlx2Explorer/Wlx2Explorer.csproj | 1 + 10 files changed, 59 insertions(+), 66 deletions(-) diff --git a/Wlx2Explorer.sln b/Wlx2Explorer.sln index 944bebf..7074eb7 100644 --- a/Wlx2Explorer.sln +++ b/Wlx2Explorer.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 15 -VisualStudioVersion = 15.0.28307.852 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.29318.209 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Wlx2Explorer", "Wlx2Explorer\Wlx2Explorer.csproj", "{82A3A166-142C-4C62-8D57-6CF8678402AA}" EndProject @@ -26,7 +26,7 @@ Global HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {BF0AC8CD-EEA6-4627-9260-407C1EA7A175} VisualSVNWorkingCopyRoot = . + SolutionGuid = {BF0AC8CD-EEA6-4627-9260-407C1EA7A175} EndGlobalSection EndGlobal diff --git a/Wlx2Explorer/Extensions/EnumExtensions.cs b/Wlx2Explorer/Extensions/EnumExtensions.cs index b1202eb..9cf4465 100644 --- a/Wlx2Explorer/Extensions/EnumExtensions.cs +++ b/Wlx2Explorer/Extensions/EnumExtensions.cs @@ -6,10 +6,7 @@ namespace Wlx2Explorer.Extensions { static class EnumExtensions { - public static string GetDescription(this Enum value) - { - var attribute = value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() as DescriptionAttribute; - return attribute?.Description; - } + public static string GetDescription(this Enum value) => + value.GetType().GetField(value.ToString()).GetCustomAttributes(typeof(DescriptionAttribute), false).OfType()?.FirstOrDefault()?.Description ?? string.Empty; } } diff --git a/Wlx2Explorer/Forms/AboutForm.cs b/Wlx2Explorer/Forms/AboutForm.cs index bcd1d12..39b2fef 100644 --- a/Wlx2Explorer/Forms/AboutForm.cs +++ b/Wlx2Explorer/Forms/AboutForm.cs @@ -12,10 +12,10 @@ partial class AboutForm : Form public AboutForm() { InitializeComponent(); - Text = "About " + AssemblyUtils.AssemblyProductName; - lblProductName.Text = string.Format("{0} v{1}", AssemblyUtils.AssemblyProductName, AssemblyUtils.AssemblyProductVersion); ; + Text = $"About {AssemblyUtils.AssemblyProductName}"; + lblProductName.Text = $"{AssemblyUtils.AssemblyProductName} v{AssemblyUtils.AssemblyProductVersion}"; lblProductTitle.Text = AssemblyUtils.AssemblyTitle; - lblCopyright.Text = "Copyright © 2014 - " + DateTime.Now.Year; + lblCopyright.Text = $"Copyright © 2014 - {DateTime.Now.Year}"; linkUrl.Text = URL; } diff --git a/Wlx2Explorer/Forms/ProgramSettingsForm.cs b/Wlx2Explorer/Forms/ProgramSettingsForm.cs index fd19350..c2f43bd 100644 --- a/Wlx2Explorer/Forms/ProgramSettingsForm.cs +++ b/Wlx2Explorer/Forms/ProgramSettingsForm.cs @@ -302,8 +302,7 @@ private void OkClick(object sender, EventArgs e) } catch { - var message = string.Format("Failed to create the file \"{0}\"", txtIniFile.Text); - MessageBox.Show(message); + MessageBox.Show($"Failed to create the file \"{txtIniFile.Text}\""); Close(); } diff --git a/Wlx2Explorer/Hooks/KeyboardHook.cs b/Wlx2Explorer/Hooks/KeyboardHook.cs index b46013f..9b61f6b 100644 --- a/Wlx2Explorer/Hooks/KeyboardHook.cs +++ b/Wlx2Explorer/Hooks/KeyboardHook.cs @@ -20,7 +20,9 @@ public bool Start(int key1, int key2, int key3) _key2 = key2; _key3 = key3; _hookProc = HookProc; - var moduleHandle = NativeMethods.GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName); + using var currentProcess = Process.GetCurrentProcess(); + using var currentModule = currentProcess.MainModule; + var moduleHandle = NativeMethods.GetModuleHandle(currentModule.ModuleName); _hookHandle = NativeMethods.SetWindowsHookEx(NativeConstants.WH_KEYBOARD_LL, _hookProc, moduleHandle, 0); var hookStarted = _hookHandle != IntPtr.Zero; return hookStarted; @@ -28,7 +30,10 @@ public bool Start(int key1, int key2, int key3) public bool Stop() { - if (_hookHandle == IntPtr.Zero) return true; + if (_hookHandle == IntPtr.Zero) + { + return true; + } var hookStoped = NativeMethods.UnhookWindowsHookEx(_hookHandle); return hookStoped; } @@ -57,10 +62,7 @@ private int HookProc(int code, IntPtr wParam, ref KBDLLHOOKSTRUCT lParam) if (key1 && key2 && lParam.vkCode == _key3) { var handler = Hooked; - if (handler != null) - { - handler.BeginInvoke(this, EventArgs.Empty, null, null); - } + handler?.BeginInvoke(this, EventArgs.Empty, null, null); } } } diff --git a/Wlx2Explorer/InitializationFile.cs b/Wlx2Explorer/InitializationFile.cs index bb31d41..84248a2 100644 --- a/Wlx2Explorer/InitializationFile.cs +++ b/Wlx2Explorer/InitializationFile.cs @@ -54,11 +54,9 @@ public void LoadFile(string fileName, Encoding fileEncoding) public void SaveFile(string fileName, Encoding fileEncoding) { - using (var file = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Read)) - using (var writer = new StreamWriter(file, fileEncoding)) - { - writer.Write(ToString()); - } + using var file = new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.Read); + using var writer = new StreamWriter(file, fileEncoding); + writer.Write(ToString()); } public override string ToString() @@ -79,30 +77,28 @@ public override string ToString() private Dictionary> Load(string fileName, Encoding fileEncoding) { var content = new Dictionary>(); - using (var file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) - using (var reader = new StreamReader(file, _fileEncoding)) + using var file = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + using var reader = new StreamReader(file, _fileEncoding); + string line = null; + string section = null; + while ((line = reader.ReadLine()) != null) { - string line = null; - string section = null; - while ((line = reader.ReadLine()) != null) + if (line.Length == 0) continue; + if (line.StartsWith(";")) continue; + if (line.Length > 2 && line[0] == '[' && line[line.Length - 1] == ']') + { + section = line.Substring(1, line.Length - 2); + if (content.ContainsKey(section)) continue; + content[section] = new Dictionary(); + } + else { - if (line.Length == 0) continue; - if (line.StartsWith(";")) continue; - if (line.Length > 2 && line[0] == '[' && line[line.Length - 1] == ']') - { - section = line.Substring(1, line.Length - 2); - if (content.ContainsKey(section)) continue; - content[section] = new Dictionary(); - } - else - { - if (section == null) continue; - var pair = line.Split('='); - if (pair.Length < 2) continue; - var keyValue = new KeyValuePair(pair[0], pair[1]); - if (content[section].Contains(keyValue)) continue; - content[section].Add(keyValue); - } + if (section == null) continue; + var pair = line.Split('='); + if (pair.Length < 2) continue; + var keyValue = new KeyValuePair(pair[0], pair[1]); + if (content[section].Contains(keyValue)) continue; + content[section].Add(keyValue); } } return content; diff --git a/Wlx2Explorer/Settings/ProgramSettings.cs b/Wlx2Explorer/Settings/ProgramSettings.cs index 1e3c40b..1ddbea6 100644 --- a/Wlx2Explorer/Settings/ProgramSettings.cs +++ b/Wlx2Explorer/Settings/ProgramSettings.cs @@ -120,11 +120,9 @@ public static void Write(ProgramSettings settings, string fileName) private static void Save(XDocument document, string fileName) { - using (TextWriter writer = new Utf8StringWriter()) - { - document.Save(writer, SaveOptions.None); - File.WriteAllText(fileName, writer.ToString()); - } + using var writer = new Utf8StringWriter(); + document.Save(writer, SaveOptions.None); + File.WriteAllText(fileName, writer.ToString()); } private class Utf8StringWriter : StringWriter diff --git a/Wlx2Explorer/StartUpManager.cs b/Wlx2Explorer/StartUpManager.cs index 57dfd0b..ece1038 100644 --- a/Wlx2Explorer/StartUpManager.cs +++ b/Wlx2Explorer/StartUpManager.cs @@ -8,30 +8,30 @@ static class StartUpManager public static void AddToStartup(string keyName, string assemblyLocation) { - using (var key = Registry.CurrentUser.OpenSubKey(RUN_LOCATION, true)) - { - key.SetValue(keyName, assemblyLocation); - } + using var key = Registry.CurrentUser.OpenSubKey(RUN_LOCATION, true); + key.SetValue(keyName, assemblyLocation); } public static void RemoveFromStartup(string keyName) { - using (var key = Registry.CurrentUser.OpenSubKey(RUN_LOCATION, true)) - { - key.DeleteValue(keyName); - } + using var key = Registry.CurrentUser.OpenSubKey(RUN_LOCATION, true); + key.DeleteValue(keyName); } public static bool IsInStartup(string keyName, string assemblyLocation) { - using (var key = Registry.CurrentUser.OpenSubKey(RUN_LOCATION, true)) + using var key = Registry.CurrentUser.OpenSubKey(RUN_LOCATION, true); + if (key == null) + { + return false; + } + var value = (string)key.GetValue(keyName); + if (string.IsNullOrEmpty(value)) { - if (key == null) return false; - var value = (string)key.GetValue(keyName); - if (string.IsNullOrEmpty(value)) return false; - var result = (value == assemblyLocation); - return result; + return false; } + var result = (value == assemblyLocation); + return result; } } } diff --git a/Wlx2Explorer/Utils/AssemblyUtils.cs b/Wlx2Explorer/Utils/AssemblyUtils.cs index baa77f3..0b1cd05 100644 --- a/Wlx2Explorer/Utils/AssemblyUtils.cs +++ b/Wlx2Explorer/Utils/AssemblyUtils.cs @@ -31,7 +31,7 @@ public static string AssemblyProductVersion get { var version = Assembly.GetExecutingAssembly().GetName().Version; - return string.Format("{0}.{1}.{2}", version.Major, version.Minor, version.Build); + return $"{version.Major}.{version.Minor}.{version.Build}"; } } } diff --git a/Wlx2Explorer/Wlx2Explorer.csproj b/Wlx2Explorer/Wlx2Explorer.csproj index 3c1b666..0c64f5b 100644 --- a/Wlx2Explorer/Wlx2Explorer.csproj +++ b/Wlx2Explorer/Wlx2Explorer.csproj @@ -13,6 +13,7 @@ v4.0 Client 512 + 9 x86