递归设置文件属性子文件夹

时间:2013-06-24 10:49:29

标签: c# .net compact-framework .net-2.0 windows-mobile-6

我在尝试使用包含设置为Readonly的文件的文件夹设置属性时出现问题。我可以设置文件夹属性,但不能设置文件。

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

2 个答案:

答案 0 :(得分: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;
        }
    }
}

答案 1 :(得分:1)

你可以创建一个递归函数来完成这项工作:

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