C# 删除硬盘指定格式文件 指定文件夹

/// <summary>
        /// 删除指定目录下的文件(一些静态文件,指定文件夹里面的子文件及文件)
        /// </summary>
        /// <param name="dir">指定目录,比如:"D://3333"</param>
        public static void DeleteFolder(string dir)
        {
            int lens = 0;
            string str = "";           
            try
            {
                foreach (string d in Directory.GetFileSystemEntries(dir))
                {
                    if (File.Exists(d))
                    {
                        lens = d.LastIndexOf(".") + 1;
                        str = d.Substring(lens, (d.Length - lens)).ToLower();
                        string[] strs = ConfigurationManager.AppSettings["FileEndName"].ToString().Split(',');
                        for (int i = 0; i < strs.Length;i++ )
                        {
                            if (str == strs[i])
                            {
                                FileInfo fi = new FileInfo(d);
                                if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                                    fi.Attributes = FileAttributes.Normal;
                                File.Delete(d);//直接删除其中的文件
                                nums++;
                            }
                        }                       
                    }
                    else
                    {
                        lens = d.LastIndexOf("//") + 1;
                        str = d.Substring(lens, d.Length - lens).ToLower();
                        string[] files = ConfigurationManager.AppSettings["FileName"].ToString().Split(',');
                        int aa = 0;
                        for (int j = 0; j < files.Length;j++ )
                        {
                            if (str == files[j])//删除文件夹为指定名称的所有内容
                            {
                                DeleteFolder1(d);
                                FileNum++;
                                aa++;
                            }                           
                        }
                        if (aa == 0)
                        {
                            DeleteFolder(d);//递归删除子文件夹
                        }
                    }
                }
                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(dir);
                if (di.GetFiles().Length + di.GetDirectories().Length == 0)
                {
                    Directory.Delete(dir);//删除已空文件夹
                    FileNum++;
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("删除失败!");
                MessageBox.Show(ex.ToString());
            }
        }

        /// <summary>
        /// 删除指定文件及里面所有的子项
        /// </summary>
        /// <param name="dir"></param>
        public static void DeleteFolder1(string dir)
        {
            try
            {
                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);//直接删除其中的文件  
                        nums++;
                    }
                    else
                        DeleteFolder1(d);//递归删除子文件夹  
                }
                Directory.Delete(dir);//删除已空文件夹
                FileNum++;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值