zoukankan      html  css  js  c++  java
  • C# 向下遍历删除子目录和子文件 及 向上遍历空的父目录

    本示例效果如下:
    根据指定的文件夹路径如E:\\a\\b\\d
    向下遍历删除其下的子文件及子目录
    删除其本身
    向上遍历删除其空的父目录

    using System.IO;

    protected void Button1_Click(object sender, EventArgs e)
    {
        string dir = "E:\\a\\b\\d";
        //string dir = "E:\\a";
        string pdir = Directory.GetParent(dir).FullName;
        DeleteFolder(dir);       
        DeletePEmptyFolder(pdir);
    }

    public void DeleteFolder(string dir)
    {
        if (Directory.Exists(dir))
        {
            foreach (string d in Directory.GetFileSystemEntries(dir))
            {
                if (File.Exists(d))
                {
                    FileInfo fi = new FileInfo(d);
                    if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                        fi.Attributes = FileAttributes.Normal;
                    File.Delete(d);//直接删除其中的文件  
                }
                else
                    DeleteFolder(d);//递归删除子文件夹  
            }
            Directory.Delete(dir);//删除已空文件夹 
        }
    }

    public void DeletePEmptyFolder(string dir)
    {
        if (Directory.GetDirectories(dir).Length == 0)
        {
            string pdir = Directory.GetParent(dir).FullName;
            if (Directory.Exists(dir))
                Directory.Delete(dir);
            DeletePEmptyFolder(pdir);
        }
    }

  • 相关阅读:
    False注入,以及SQL注入技巧总结
    xdebug插件攻击
    RPO攻击
    教务处sso设计缺陷
    MD5的Hash长度扩展攻击
    AMAZON PRICE TRACKER, AMAZON PRICE HISTORY, AMAZON PRICE DROP ALERT | DROPGG.COM
    弄懂JDK、JRE和JVM之间的联系。
    MyBatis配置及学习
    Cannot forward after response has been committed 异常原因
    bbs论坛中的问题和心得。(更新)
  • 原文地址:https://www.cnblogs.com/zhwl/p/2769906.html
Copyright © 2011-2022 走看看