I have a requirement where I want to get the Environment.SpecialFolder
value from a file path.
Eg -
string filePath = @"C:\Program Files (x86)\text.txt"
//SpecialFolder sf = GetSpecialFolderAssociatedWithPath(filePath); // sf will be ProgramFilesX86
//Need something similar
I want to further use the sf
to generate another path, so if we get the path corresponding to that particular SpecialFolder
, that will work as well.
You could do it something like this (assuming you want to get the actual enum value for the special folder):
Note that I had to return a nullable because Microsoft failed to follow their own guidelines and didn't include a special "None" zero value in the
Environment.SpecialFolder
enum that I could return to indicate "not found".The usage would be something like this:
If you want both the path and the enum value, you could return them both in a tuple:
Which you could use like:
Actually, it's possible that some of the special folders may be nested underneath other special folders, for example you might have:
In that event, the code above will return the first path that it matches, rather than the longest; i.e. looking for
"C:\Path1\Path2\Path3\SomeFile.txt"
might return the special folder for"C:\Path1\Path2"
rather than the one for"C:\Path1\Path2\Path3"
.If you want to handle that possibility, you'll have to find the longest matching path, for example:
And use it like:
Another thing to be aware of is that multiple special folders might have the same path. In this case it's not possible to differentiate them, so the code will just return the first match it finds.
Also note the use of
filePath = Path.GetFullPath(filePath);
to ensure that relative paths are converted to absolute paths, otherwise the matching likely wouldn't work.