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

阅读更多

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);


}


}


你可能感兴趣的:(C#批量删除指定文件夹下指定文件名的所有文件夹)