Skip to content

Commit be56b2c

Browse files
authored
Merge pull request #115 from Jack251970/fix_null
Enable nullable reference types and improve file path handling
2 parents 4ba3513 + 35af4ec commit be56b2c

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

ContextMenuManager/MainWindow.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ private void SetSearchEnabled(bool enabled)
500500

501501
private void RefreshButton_Click(object sender, RoutedEventArgs e)
502502
{
503-
ObjectPath.FilePathDic.Clear();
503+
ObjectPath.ClearFilePathDic();
504504
AppConfig.ReloadConfig();
505505
GuidInfo.ReloadDics();
506506
XmlDicHelper.ReloadDics();

ContextMenuManager/Methods/ObjectPath.cs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
using System.IO;
66
using System.Linq;
77

8+
#nullable enable
9+
810
namespace ContextMenuManager.Methods
911
{
1012
internal static class ObjectPath
@@ -22,9 +24,9 @@ public enum PathType { File, Directory, Registry }
2224
/// <remarks>fileName为Win+R、注册表等可直接使用的文件名</remarks>
2325
/// <param name="fileName">文件名</param>
2426
/// <returns>成功提取返回true, fullPath为现有文件路径; 否则返回false, fullPath为null</returns>
25-
public static bool GetFullFilePath(string fileName, out string fullPath)
27+
public static bool GetFullFilePath(string fileName, out string? fullPath)
2628
{
27-
fullPath = string.Empty;
29+
fullPath = null;
2830
if (string.IsNullOrWhiteSpace(fileName)) return false;
2931

3032
foreach (var name in new[] { fileName, $"{fileName}.exe" })
@@ -37,25 +39,30 @@ public static bool GetFullFilePath(string fileName, out string fullPath)
3739
if (File.Exists(fullPath)) return true;
3840
}
3941

40-
fullPath = Registry.GetValue($@"{RegAppPath}\{name}", "", null)?.ToString() ?? string.Empty;
42+
fullPath = Registry.GetValue($@"{RegAppPath}\{name}", "", null)?.ToString();
4143
if (File.Exists(fullPath)) return true;
4244
}
43-
fullPath = string.Empty;
45+
fullPath = null;
4446
return false;
4547
}
4648

47-
public static readonly ConcurrentDictionary<string, string> FilePathDic = new(StringComparer.OrdinalIgnoreCase);
49+
private static readonly ConcurrentDictionary<string, string?> FilePathDic = new(StringComparer.OrdinalIgnoreCase);
50+
51+
public static void ClearFilePathDic()
52+
{
53+
FilePathDic.Clear();
54+
}
4855

4956
/// <summary>从包含现有文件路径的命令语句中提取文件路径</summary>
5057
/// <param name="command">命令语句</param>
5158
/// <returns>成功提取返回现有文件路径,否则返回值为null</returns>
52-
public static string ExtractFilePath(string command)
59+
public static string? ExtractFilePath(string command)
5360
{
54-
if (string.IsNullOrWhiteSpace(command)) return string.Empty;
61+
if (string.IsNullOrWhiteSpace(command)) return null;
5562
if (FilePathDic.TryGetValue(command, out var value)) return value;
5663
else
5764
{
58-
var filePath = string.Empty;
65+
string? filePath;
5966
var partCmd = Environment.ExpandEnvironmentVariables(command).Replace(@"\\", @"\");
6067
if (partCmd.StartsWith(ShellExecuteCommand, StringComparison.OrdinalIgnoreCase))
6168
{
@@ -66,7 +73,7 @@ public static string ExtractFilePath(string command)
6673
var fileName = arr[0];
6774
if (GetFullFilePath(fileName, out filePath))
6875
{
69-
FilePathDic.TryAdd(command, filePath);
76+
FilePathDic.TryAdd(command, null);
7077
return filePath;
7178
}
7279
if (arr.Length > 1)
@@ -134,7 +141,7 @@ public static string ExtractFilePath(string command)
134141
}
135142
while (index != -1);
136143
}
137-
FilePathDic.TryAdd(command, string.Empty);
144+
FilePathDic.TryAdd(command, null);
138145
return null;
139146
}
140147
}

0 commit comments

Comments
 (0)