最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#實現(xiàn)安全刪除文件目錄的方法

 更新時間:2015年05月20日 11:08:14   作者:秋風(fēng)秋雨  
這篇文章主要介紹了C#實現(xiàn)安全刪除文件目錄的方法,涉及C#刪除文件目錄的相關(guān)技巧,非常具有實用價值,需要的朋友可以參考下

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

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

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

在本例中,若刪除過程中被其他進程引用,等待并循環(huán)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) + "\\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#程序設(shè)計有所幫助。

相關(guān)文章

最新評論

福建省| 罗江县| 库伦旗| 阿图什市| 兰考县| 崇礼县| 兰州市| 蛟河市| 潮州市| 东至县| 巴林左旗| 盐池县| 台北市| 巴南区| 恩平市| 威远县| 桦南县| 连平县| 中卫市| 盖州市| 新乡县| 黄骅市| 青冈县| 吉木萨尔县| 霍邱县| 佳木斯市| 清苑县| 久治县| 胶南市| 玉山县| 普安县| 海城市| 北票市| 渝北区| 梨树县| 德阳市| 通化县| 即墨市| 蒙阴县| 嵊州市| 屏山县|