国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

C#實現安全刪除文件目錄的方法

2020-01-24 01:49:56
字體:
來源:轉載
供稿:網友

本文實例講述了C#實現安全刪除文件目錄的方法。分享給大家供大家參考。具體分析如下:

1. 創建文件夾 (簡單,沒多考慮)

2. 刪除所建文件夾:為防止刪除過程中有其他進程引用該文件夾中文件,增加了對此意外情況的考慮。

在本例中,若刪除過程中被其他進程引用,等待并循環5次嘗試再次刪除操作。長時間無法被刪除,則刪除文件目錄失敗

using System;using System.IO;namespace Retry{ class Program { static void Main(string[] args) {  DirectoryInfo dirInfo = Directory.CreateDirectory(@"C:/TestDir");  string folderName = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "http://TestDir";  if (!Directory.Exists(folderName))  {  Directory.CreateDirectory(folderName);  Console.WriteLine("{0} created! ",folderName);  }  int retryTimes = 1;  do  {  if (Directory.Exists(folderName))  {   try   {   Console.WriteLine("Tring to delete file the {0} time.",retryTimes);   Directory.Delete(folderName, true);   Console.WriteLine("Deleting file successfully.");   break;   }   catch (IOException ex)   {   Console.WriteLine("Exception! ", ex.ToString());   Console.WriteLine("Sleep 5 seconds and retry.");   System.Threading.Thread.Sleep(5000);   retryTimes++;   }  }  else  {   Console.WriteLine("Delete folder successfully");   break;  }  } while (retryTimes <= 5);  if (Directory.Exists(folderName))  Console.WriteLine("Deleting folder failed.");  Console.WriteLine("Done");  Console.ReadKey(); } }}

希望本文所述對大家的C#程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 灵山县| 金堂县| 星座| 沁源县| 高安市| 闽侯县| 谢通门县| 花垣县| 东乡族自治县| 南丰县| 蒙自县| 兴文县| 务川| 西平县| 会同县| 贵溪市| 彰化县| 通海县| 新余市| 台东县| 绥棱县| 烟台市| 贞丰县| 开鲁县| 黎城县| 保定市| 徐水县| 莒南县| 镇原县| 万全县| 香港| 九龙城区| 磴口县| 天柱县| 新昌县| 大化| 靖江市| 左云县| 安达市| 怀集县| 屏山县|