我在尝试使用文件夹设置属性时遇到问题,这些文件夹中的文件设置为只读。我可以设置文件夹属性,但不能设置其中的文件。
这将设置包括子文件夹的属性,然后删除。在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();
}
}
}