Vano Maisuradze
Vano Maisuradze

Reputation: 5909

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.

Upvotes: 3

Views: 2563

Answers (5)

Xan-Kun Clark-Davis
Xan-Kun Clark-Davis

Reputation: 2843

I ended up using it this way:

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;
}

If handling strings from dubious sources (the user :-) ), there are three caveats to keep in mind:

  • Path.Combine vs. Path.Join, since they handle absolute paths (or paths that look like absolute paths) differently.
  • Path.GetFullPath, which takes a string an produces the full and normalized version of it.
  • GetFolderPath can return an empty string, which generates a System.ArgumentException: 'The path is empty. (Parameter 'path')' when used for creating a DirectoryInfo.

I like to keep this logic outside the method, but I am not sure if the OrdinalIgnoreCase or any other normalization is still necessary. I guess not.

P.S.: I think in modern lingo the method should be called TrySpecialFolder or something :-)

Upvotes: 0

nemethv
nemethv

Reputation: 47

I don't have enough reputation to add a comment so as a +1 to BobRassler's answer, string comparisons might be more useful.

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
}

Upvotes: 1

BobRassler
BobRassler

Reputation: 31

I'm afraid the answers given seem to be the only way, I hate the special folders because what ought to be a very simple function -

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);
  }
}

Becomes ugly because you have to include

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

Fair enough Microsoft, to have a folder that could exist anywhere, on a remote PC, on a server etc. But really what is wrong with the UNIX/Linux way, use links to folder and if the destination physical folder has to move, alter the link. Then you can itterate them in a nice neat function treating them all as if ordinary folders.

Upvotes: 1

Saber Amani
Saber Amani

Reputation: 6489

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.

Upvotes: 6

Tigran
Tigran

Reputation: 62265

Use a reflection to get all values from that enum, like here http://geekswithblogs.net/shahed/archive/2006/12/06/100427.aspx and check against collection of generated paths you get.

Upvotes: 0

Related Questions