C# 文件类的操作---删除

 

//删除文件夹
1这是最简单的方法
 
        DirectoryInfo di = new DirectoryInfo(string Path);
         di.Delete(true);
  注:path是你要删除的非空目录;
     true:你要删除里面所有的文件,包括文件夹和子文件夹
2
 /// <summary>
       /// 删除非空文件夹
       /// </summary>
       /// <param name="path">要删除的文件夹目录</param>
        void DeleteDirectory(string path)
        {
            DirectoryInfo dir = new DirectoryInfo(path); 
            if (dir.Exists)
            {
                DirectoryInfo[] childs = dir.GetDirectories();
                foreach (DirectoryInfo child in childs)
                {
                    child.Delete(true);
                }
                dir.Delete(true);
            }
        }
 
//删除文件
1.递归方法:
private staticv oid DeleteDirectory(File tmpFile) {
        if (!tmpFile.exists()) {
             System.out.println("file is not exist!");
            return;
         }
        if (tmpFile.isDirectory()) {
             File[] fileList = tmpFile.listFiles();
            for (int i =0; i < fileList.length; i++) {
                if (fileList[i].isDirectory()) {
                     DeleteDirectorys(fileList[i]);s
                 }  
           }
     }
}

 

posted @ 2017-03-14 14:41  听雨的人  阅读(549)  评论(0编辑  收藏  举报