Skip to content

Commit

Permalink
Upgraded to C# 9
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderPro committed Aug 28, 2023
1 parent bdd3159 commit 1e1115d
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 66 deletions.
6 changes: 3 additions & 3 deletions Wlx2Explorer.sln
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
7 changes: 2 additions & 5 deletions Wlx2Explorer/Extensions/EnumExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DescriptionAttribute>()?.FirstOrDefault()?.Description ?? string.Empty;
}
}
6 changes: 3 additions & 3 deletions Wlx2Explorer/Forms/AboutForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
3 changes: 1 addition & 2 deletions Wlx2Explorer/Forms/ProgramSettingsForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
14 changes: 8 additions & 6 deletions Wlx2Explorer/Hooks/KeyboardHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,20 @@ 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;
}

public bool Stop()
{
if (_hookHandle == IntPtr.Zero) return true;
if (_hookHandle == IntPtr.Zero)
{
return true;
}
var hookStoped = NativeMethods.UnhookWindowsHookEx(_hookHandle);
return hookStoped;
}
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
50 changes: 23 additions & 27 deletions Wlx2Explorer/InitializationFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -79,30 +77,28 @@ public override string ToString()
private Dictionary<string, IDictionary<string, string>> Load(string fileName, Encoding fileEncoding)
{
var content = new Dictionary<string, IDictionary<string, string>>();
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<string, string>();
}
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<string, string>();
}
else
{
if (section == null) continue;
var pair = line.Split('=');
if (pair.Length < 2) continue;
var keyValue = new KeyValuePair<string, string>(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<string, string>(pair[0], pair[1]);
if (content[section].Contains(keyValue)) continue;
content[section].Add(keyValue);
}
}
return content;
Expand Down
8 changes: 3 additions & 5 deletions Wlx2Explorer/Settings/ProgramSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 14 additions & 14 deletions Wlx2Explorer/StartUpManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
2 changes: 1 addition & 1 deletion Wlx2Explorer/Utils/AssemblyUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Wlx2Explorer/Wlx2Explorer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<TargetFrameworkProfile>Client</TargetFrameworkProfile>
<FileAlignment>512</FileAlignment>
<LangVersion>9</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
<PlatformTarget>x86</PlatformTarget>
Expand Down

0 comments on commit 1e1115d

Please sign in to comment.