C# 检查DirectoryInfo.FullName是否为特殊文件夹

C# 检查DirectoryInfo.FullName是否为特殊文件夹,c#,special-folders,directoryinfo,C#,Special Folders,Directoryinfo,我的目标是检查DirectoryInfo.FullName是否是特殊文件夹之一 下面是我为此所做的(如果每个特殊文件夹相等,请检查directoryInfo.FullName): 但是有很多特殊的文件夹(Cookies、ApplicationData、InternetCache等)。有没有办法更有效地完成这项任务 谢谢。使用反射从该枚举中获取所有值,如此处所示,并检查生成的路径集合。尝试以下代码: bool result = false; DirectoryInf

我的目标是检查DirectoryInfo.FullName是否是特殊文件夹之一

下面是我为此所做的(如果每个特殊文件夹相等,请检查directoryInfo.FullName):

但是有很多特殊的文件夹(Cookies、ApplicationData、InternetCache等)。有没有办法更有效地完成这项任务


谢谢。

使用反射从该枚举中获取所有值,如此处所示,并检查生成的路径集合。

尝试以下代码:

        bool result = false;
        DirectoryInfo directoryInfo = new DirectoryInfo("Directory path");
        foreach (Environment.SpecialFolder suit in Enum.GetValues(typeof(Environment.SpecialFolder)))
        {
            if (directoryInfo.FullName == Environment.GetFolderPath(suit))
            {
                result = true;
                break;
            }
        }

        if (result)
        {
            // Do what ever you want
        }

希望能有所帮助。

恐怕给出的答案似乎是唯一的方法,我讨厌特殊的文件夹,因为它应该是一个非常简单的功能-

void CollectFiles(string strDir, string pattern) {
  DirectoryInfo di = new DirectoryInfo(strDir);
  foreach(FileInfo fi in di.GetFiles(pattern) {
    //store file data
  }
  foreach(DirectoryInfo diInfo in di.GetDirectories()) {
    CollectFiles(diInfo);
  }
}
变得丑陋是因为你必须包括

Check If This Is A Special Folder And Deal With It And Its Child Folders Differently ();
公平的说,微软有一个文件夹,可以存在于任何地方,在远程PC上,在服务器上等等。但UNIX/Linux方式的真正问题是,使用指向文件夹的链接,如果目标物理文件夹必须移动,请更改链接。然后,您可以在一个漂亮整洁的函数中对它们进行分类,将它们都当作普通文件夹处理

Check If This Is A Special Folder And Deal With It And Its Child Folders Differently ();