У меня возникли проблемы с попыткой установить атрибуты для папок, внутри которых были файлы, для которых было установлено значение «Только чтение». Я мог установить атрибуты папки, но не файлы внутри.
Это установит атрибуты, включая подпапки, а затем удалит. Нашла информацию о codeproject.
использовать это:
ScanDirectory scanDirectory = new ScanDirectory();
scanDirectory.WalkDirectory(@"/Storage Card");
public class ScanDirectory
{
public void WalkDirectory(string directory)
{
WalkDirectory(new DirectoryInfo(directory));
}
private void WalkDirectory(DirectoryInfo directory)
{
// Scan all files in the current path
foreach (FileInfo file in directory.GetFiles())
{
file.Attributes &= ~FileAttributes.ReadOnly;
file.Delete();
}
DirectoryInfo[] subDirectories = directory.GetDirectories();
// Scan the directories in the current directory and call this method
// again to go one level into the directory tree
foreach (DirectoryInfo subDirectory in subDirectories)
{
WalkDirectory(subDirectory);
subDirectory.Attributes &= ~FileAttributes.ReadOnly;
subDirectory.Delete();
}
}
}