简体   繁体   English

如何递归获取所有非系统目录?

[英]How can I recursively get all non-system directories?

I have recently disabled 8.3 file names on my NTFS volume and noticed a significant decrease (it only takes 25% as much time now) in the amount of time it takes to enumerate a new directory with a very large amount of files. 我最近在NTFS卷上禁用了8.3文件名,并注意到枚举包含大量文件的新目录所花费的时间显着减少(现在仅花费25%的时间)。 However, this does not apply to existing files. 但是,这不适用于现有文件。

To change that, I want to create an exe that will recursively go through all of the files on the drive that are not in system folders, move them to a temp directory, and move them back in order to force the 8.3 file name removal on them. 要更改此设置,我想创建一个exe文件,它将以递归方式遍历驱动器上所有不在系统文件夹中的文件,将它们移至temp目录,然后再移回以强制删除8.3文件名。他们。 I already know how to enumerate through the files of a directory and perform this action on each of them, but I'm not quite sure how to get a list of all the directories on the disk without any system directories included. 我已经知道如何枚举目录文件并对每个文件执行此操作,但是我不太确定如何获取磁盘上所有目录的列表,而不包括任何系统目录。 Is there an Attribute I can look for within a DirectoryInfo object? 我可以在DirectoryInfo对象中寻找属性吗? If not, what other approach can I take to accomplish this? 如果没有,我可以采取什么其他方法来完成此任务?

Here you go. 干得好。 I believe this is what you are after... See FileAttributes for more information. 我相信这是您的追求...有关更多信息,请参见FileAttributes

public void RecursivePathWalk(string directory)
{
    string[] filePaths = Directory.GetFiles(directory);
    foreach (string filePath in filePaths)
    {
        if (IsSystem(filePath))
            continue;

        DoWork(filePath);
    }

    string[] subDirectories = Directory.GetDirectories(directory);
    foreach (string subDirectory in subDirectories)
    {
        if (IsSystem(subDirectory))
            continue;

        RecursivePathWalk(subDirectory);
    }
}

public void DoWork(string filePath)
{
    //Your logic here
}

public bool IsSystem(string path)
{
    FileAttributes attributes = File.GetAttributes(path);
    return (attributes & FileAttributes.System) != 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM