3

我在尝试使用文件夹设置属性时遇到问题,这些文件夹中的文件设置为只读。我可以设置文件夹属性,但不能设置其中的文件。

这将设置包括子文件夹的属性,然后删除。在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();
        }
    }
}
4

2 回答 2

3

You could create a recursive function to do the job:

    private void UpdateFileAttributes(DirectoryInfo dInfo)
    {
        // Set Directory attribute
        dInfo.Attributes &= ~FileAttributes.ReadOnly;

        // get list of all files in the directory and clear 
        // the Read-Only flag

        foreach (FileInfo file in dInfo.GetFiles())
        {
            file.Attributes &= ~FileAttributes.ReadOnly;
        }

        // recurse all of the subdirectories
        foreach (DirectoryInfo subDir in dInfo.GetDirectories())
        {
            UpdateFileAttributes(subDir);
        }
    }
于 2013-10-14T18:29:27.917 回答
1

您可以使用深度优先搜索(堆栈版本)或呼吸优先搜索(队列版本)进行树遍历来查看您的文件夹,如下所示:

static void Main(String[] args)
{
    // Using breath-first search (BFS)
    Queue<DirectoryInfo> myQueue = new Queue<DirectoryInfo>();

    // Your storage path here
    myQueue.Enqueue(new DirectoryInfo("C:\\"));

    while(myQueue.Count > 0)
    {
        // Look at the current directory
        DirectoryInfo crtDI = myQueue.Dequeue();

        // Put the directories from the crt dir in a queue
        foreach (DirectoryInfo otherDir in GetCrtDirectories(crtDI))
            myQueue.Enqueue(otherDir);

        // Do what you want with the directory here 
        FixAccess(crtDI);
    }
}

private static IEnumerable<DirectoryInfo> GetCrtDirectories(DirectoryInfo crtDI)
{
    foreach(string dirStr in Directory.GetDirectories(crtDI.FullName))
    {
        DirectoryInfo newDir = new DirectoryInfo(dirStr);
        yield return newDir;
    }
}

private static void FixAccess (DirectoryInfo DI)
{
    foreach (string fileName in System.IO.Directory.GetFiles(DI.FullName))
    {
        FileInfo fileInfo = new FileInfo(fileName);
        FileAttributes attributes = fileInfo.Attributes;

        if ((attributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly)
        {
            // set the attributes to nonreadonly
            fileInfo.Attributes &= ~FileAttributes.ReadOnly;
        }
    }
}
于 2013-06-24T11:02:38.110 回答