检查DirectoryInfo.FullName是一个特殊文件夹

本文关键字:一个 文件夹 DirectoryInfo FullName 检查 | 更新日期: 2023-09-27 18:06:25

我的目标是检查,如果DirectoryInfo。FullName是一个特殊的文件夹。

这是我所做的(检查directoryInfo。FullName到每个特殊文件夹(如果它们相等):

        DirectoryInfo directoryInfo = new DirectoryInfo("Directory path");
        if (directoryInfo.FullName == Environment.GetFolderPath(Environment.SpecialFolder.Windows) ||
            directoryInfo.FullName == Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles ||) 
            ...
            ...
           )
        {
            // directoryInfo is the special folder
        }

但是有许多特殊的文件夹(Cookies, ApplicationData, InternetCache等)。有什么办法能更有效地完成这项任务吗?

谢谢。

检查DirectoryInfo.FullName是一个特殊文件夹

试试下面的代码:

        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的真正错误之处在于,使用到文件夹的链接,如果目标物理文件夹必须移动,则更改链接。然后,您可以在一个简洁的函数中对它们进行迭代,将它们视为普通文件夹。

我没有足够的声誉来添加评论,所以作为BobRassler回答的+1,字符串比较可能更有用。

bool isSpecialFolder = false;
DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(tbx_FolderName.Text, fileName));
foreach (Environment.SpecialFolder specialFolder in Enum.GetValues(typeof(Environment.SpecialFolder)))
{
    if (directoryInfo.FullName.ToString()
            .ToLower() ==
        Environment.GetFolderPath(specialFolder)
            .ToLower())
    {
        isSpecialFolder = true;
        break;
    }
}
if (isSpecialFolder)
{
    // something 
}
else
{
    // something else
}

使用反射从该枚举中获取所有值,如http://geekswithblogs.net/shahed/archive/2006/12/06/100427.aspx,并检查您获得的生成路径集合

我最终这样使用它:

public static bool IsSpecialFolder(DirectoryInfo directoryInfo, out Environment.SpecialFolder? _specialFolder) {
    bool isSpecialFolder = false;
    _specialFolder = null;
    string directoryInfo_FullPath = directoryInfo.FullName;
    foreach (Environment.SpecialFolder specialFolder in Enum.GetValues(typeof(Environment.SpecialFolder))) {
        var specialFolder_FullPath = Environment.GetFolderPath(specialFolder);
        if (string.Equals(directoryInfo_FullPath, specialFolder_FullPath, StringComparison.OrdinalIgnoreCase)) {
            isSpecialFolder = true;
            _specialFolder  = specialFolder;
            break;
        }
    }
    return isSpecialFolder;
}

如果处理来自可疑来源的字符串(用户:-)),需要记住三个注意事项:

  • Path.CombinePath.Join,因为它们处理绝对路径(或看起来像绝对路径的路径)不同。
  • Path.GetFullPath,它接受一个字符串,并产生它的完整和规范化版本。
  • GetFolderPath可以返回一个空字符串,用于创建DirectoryInfo时生成System.ArgumentException: 'The path is empty. (Parameter 'path')'

我喜欢将此逻辑保留在方法之外,但我不确定OrdinalIgnoreCase或任何其他规范化是否仍然必要。我想没有。

注::我认为在现代术语中,该方法应该称为TrySpecialFolder或其他东西:-)