C#批量删除指定文件夹下指定文件名的所有文件夹

来源:互联网 发布:cygwin arm linux gcc 编辑:程序博客网 时间:2024/05/18 03:41
private void DeleteDirByName(string rootPath, string name)        {            string dirName = rootPath;            if(rootPath.EndsWith("//")||rootPath.EndsWith("/"))            {                rootPath = rootPath.Substring(0,rootPath.Length-1);            }            int indexSplit = rootPath.LastIndexOf('//');            if(indexSplit<0)            {                indexSplit = rootPath.LastIndexOf('/');            }            if(indexSplit>0)            {                dirName = rootPath.Substring(indexSplit + 1);            }            if (dirName.ToLower() == name.ToLower())            {                this.SetFileAttributes(rootPath);                Directory.Delete(rootPath, true);                this.textBox3.Text += rootPath + Environment.NewLine;            }            else            {                string[] subDirs = Directory.GetDirectories(rootPath);                foreach (string subDir in subDirs)                {                    this.DeleteDirByName(subDir, name);                }            }        }        private void SetFileAttributes(string path)        {            string[] files = Directory.GetFiles(path);            foreach (string file in files)            {                File.SetAttributes(file, FileAttributes.Normal);            }            string[] subDirs = Directory.GetDirectories(path);            foreach (string subDir in subDirs)            {                this.SetFileAttributes(subDir);            }        }

原文地址:点击打开链接
1 0
原创粉丝点击