Search code examples
c#special-foldersdirectoryinfo

Check if DirectoryInfo.FullName is special folder


My goal is to check, if DirectoryInfo.FullName is one of the special folders.

Here is what I'm doing for this (Check directoryInfo.FullName to each special folder if they are equal):

        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
        }

But there are many special folders (Cookies, ApplicationData, InternetCache, etc.). Is there any way to do this task more efficiently?

Thanks.


Solution

  • Try this following code :

            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
            }
    

    hope this help.