55using System . IO ;
66using System . Linq ;
77
8+ #nullable enable
9+
810namespace 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