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

Van*_*dze 3 c# directoryinfo special-folders

我的目标是检查 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
        }
Run Code Online (Sandbox Code Playgroud)

但还有很多特殊的文件夹(Cookies、ApplicationData、InternetCache 等)。有什么办法可以更有效地完成这项任务吗?

谢谢。

sab*_*ber 6

尝试以下代码:

        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
        }
Run Code Online (Sandbox Code Playgroud)

希望这有帮助。