@@ -49,7 +49,7 @@ public static string CacheDirectory
4949
5050#if ! NETFRAMEWORK
5151
52- static readonly object lock_GetCacheFilePath = new ( ) ;
52+ static readonly System . Threading . Lock lock_GetCacheFilePath = new ( ) ;
5353
5454 /// <summary>
5555 /// 根据缓存子文件夹名称与文件扩展名获取一个缓存文件路径
@@ -82,18 +82,6 @@ public static string GetCacheFilePath(string dirName, string fileNamePrefix, str
8282 }
8383 }
8484
85- /// <summary>
86- /// 尝试延时一段时间后删除文件
87- /// </summary>
88- /// <param name="filePath">要删除的文件路径</param>
89- /// <param name="millisecondsDelay">延时等待的毫秒数</param>
90- [ Obsolete ( "use TryDeleteInDelayAsync" ) ]
91- public static async void TryDeleteInDelay ( string filePath , int millisecondsDelay = 9000 )
92- {
93- await Task . Delay ( millisecondsDelay ) ;
94- FileTryDelete ( filePath ) ;
95- }
96-
9785 /// <summary>
9886 /// 尝试延时一段时间后删除文件
9987 /// </summary>
@@ -105,35 +93,6 @@ public static async Task TryDeleteInDelayAsync(string filePath, int milliseconds
10593 FileTryDelete ( filePath ) ;
10694 }
10795
108- /// <summary>
109- /// 启动进程后尝试延时一段时间后删除文件
110- /// </summary>
111- /// <param name="process">启动的进程</param>
112- /// <param name="filePath">要删除的文件路径</param>
113- /// <param name="millisecondsDelay">延时等待的毫秒数</param>
114- /// <param name="processWaitMillisecondsDelay">启动的进程等待退出的毫秒数</param>
115- [ Obsolete ( "use TryDeleteInDelayAsync" ) ]
116- public static void TryDeleteInDelay ( Process ? process , string filePath , int millisecondsDelay = 9000 , int processWaitMillisecondsDelay = 9000 )
117- {
118- if ( process != null )
119- {
120- var waitForExitResult = process . TryWaitForExit ( processWaitMillisecondsDelay ) ;
121- if ( ! waitForExitResult )
122- {
123- try
124- {
125- process . KillEntireProcessTree ( ) ;
126- }
127- catch
128- {
129- }
130- TryDeleteInDelay ( filePath , millisecondsDelay ) ;
131- return ;
132- }
133- }
134- FileTryDelete ( filePath ) ;
135- }
136-
13796 /// <summary>
13897 /// 启动进程后尝试延时一段时间后删除文件
13998 /// </summary>
@@ -196,140 +155,6 @@ protected static void InitFileSystem(Func<string> getAppDataDirectory, Func<stri
196155 IOPath . getAppDataDirectory = getAppDataDirectory ;
197156 IOPath . getCacheDirectory = getCacheDirectory ;
198157 }
199-
200- #if ! NETFRAMEWORK
201- /// <summary>
202- /// 带迁移的初始化文件系统,使用 <see cref="Directory.Move(string, string)"/> 或 xcopy 进行移动,如果迁移失败则回退源目录
203- /// </summary>
204- /// <param name="destAppDataPath">新的 AppData 文件夹路径</param>
205- /// <param name="destCachePath">新的 Cache 文件夹路径</param>
206- /// <param name="sourceAppDataPath">旧的 AppData 文件夹路径</param>
207- /// <param name="sourceCachePath">旧的 Cache 文件夹路径</param>
208- protected static void InitFileSystemWithMigrations (
209- string destAppDataPath , string destCachePath ,
210- string sourceAppDataPath , string sourceCachePath )
211- {
212- bool ExistsNotEmptyDir ( string path )
213- {
214- var exists = Directory . Exists ( path ) ;
215- if ( OSHelper . IsPublishToStore )
216- {
217- if ( path == destCachePath || path == sourceCachePath )
218- {
219- return false ;
220- }
221- }
222- return exists && Directory . EnumerateFileSystemEntries ( path ) . Any ( ) ; // 文件夹存在且不为空文件夹
223- }
224-
225- var paths = new [ ] { destAppDataPath , destCachePath , } ;
226- var dict_paths = paths . ToDictionary ( x => x , x => ExistsNotEmptyDir ( x ) ) ;
227-
228- if ( dict_paths . Values . All ( x => ! x ) )
229- {
230- var old_paths = new [ ] { sourceAppDataPath , sourceCachePath , } ;
231- if ( old_paths . All ( x => Directory . Exists ( x ) && Directory . EnumerateFileSystemEntries ( x ) . Any ( ) ) ) // 迁移之前根目录上的文件夹
232- {
233- var isNotFirst = false ;
234- for ( int i = 0 ; i < old_paths . Length ; i ++ )
235- {
236- var path = paths [ i ] ;
237- var old_path = old_paths [ i ] ;
238- try
239- {
240- if ( ! isNotFirst )
241- {
242- try
243- {
244- // 尝试搜索之前版本的进程将其结束
245- var currentProcess = Process . GetCurrentProcess ( ) ;
246- var query = from x in Process . GetProcessesByName ( currentProcess . ProcessName )
247- where x != currentProcess
248- let m = x . TryGetMainModule ( )
249- where m != null && m . FileName != currentProcess . TryGetMainModule ( ) ? . FileName
250- select x ;
251- var process = query . ToArray ( ) ;
252- foreach ( var proces in process )
253- {
254- try
255- {
256- #if NETCOREAPP3_0_OR_GREATER
257- proces . Kill ( true ) ;
258- #else
259- proces . Kill ( ) ;
260- #endif
261- }
262- catch
263- {
264- }
265- }
266- }
267- catch
268- {
269- }
270- isNotFirst = true ;
271- }
272- MoveDirectory ( old_path , path ) ;
273- dict_paths [ path ] = true ;
274- }
275- catch
276- {
277- if ( ! OSHelper . IsPublishToStore )
278- {
279- // 跨卷移动失败或其他原因失败,使用旧的目录,并尝试删除创建的空文件夹
280- DirTryDelete ( path ) ;
281- }
282- paths [ i ] = old_path ;
283- }
284- }
285- }
286- }
287-
288- foreach ( var item in dict_paths )
289- {
290- if ( ! item . Value )
291- {
292- Directory . CreateDirectory ( item . Key ) ;
293- }
294- }
295-
296- InitFileSystem ( GetAppDataDirectory , GetCacheDirectory ) ;
297- string GetAppDataDirectory ( ) => paths [ 0 ] ;
298- string GetCacheDirectory ( ) => paths [ 1 ] ;
299- }
300- #endif
301-
302- /// <summary>
303- /// 初始化文件系统,但优先使用旧目录上的文件夹,如果存在的话(允许空文件夹),不会进行文件迁移
304- /// </summary>
305- /// <param name="destAppDataPath">新的 AppData 文件夹路径</param>
306- /// <param name="destCachePath">新的 Cache 文件夹路径</param>
307- /// <param name="sourceAppDataPath">旧的 AppData 文件夹路径</param>
308- /// <param name="sourceCachePath">旧的 Cache 文件夹路径</param>
309- protected static void InitFileSystemUseDestFirst (
310- string destAppDataPath , string destCachePath ,
311- string sourceAppDataPath , string sourceCachePath )
312- {
313- var paths = new [ ] { destAppDataPath , destCachePath , } ;
314- var old_paths = new [ ] { sourceAppDataPath , sourceCachePath , } ;
315-
316- for ( int i = 0 ; i < old_paths . Length ; i ++ )
317- {
318- var item = old_paths [ i ] ;
319- if ( Directory . Exists ( item ) )
320- {
321- paths [ i ] = item ;
322- }
323- else
324- {
325- DirCreateByNotExists ( paths [ i ] ) ;
326- }
327- }
328-
329- InitFileSystem ( GetAppDataDirectory , GetCacheDirectory ) ;
330- string GetAppDataDirectory ( ) => paths [ 0 ] ;
331- string GetCacheDirectory ( ) => paths [ 1 ] ;
332- }
333158 }
334159
335160 /// <summary>
0 commit comments