Reputation: 1736
All folders have a type: Most of them are named "File folder", but some are called "Mediaserver" or "Local harddrive" (Translations). How do I retrieve those folder types using C#? I found this for files: How can I get the description of a file extension in .NET
Upvotes: 8
Views: 795
Reputation: 73502
SHGetFileInfo is the function you need. You need to pass the flag FILE_ATTRIBUTE_DIRECTORY
as parameter to dwFileAttributes
parameter.
Based on the same answer you linked I've modified the code to make it work for directories.
public static string GetFileFolderTypeDescription(string fileNameOrExtension)
{
SHFILEINFO shfi;
if (IntPtr.Zero != SHGetFileInfo(
fileNameOrExtension,
FILE_ATTRIBUTE_NORMAL | FILE_ATTRIBUTE_DIRECTORY,
out shfi,
(uint)Marshal.SizeOf(typeof(SHFILEINFO)),
SHGFI_USEFILEATTRIBUTES | SHGFI_TYPENAME))
{
return shfi.szTypeName;
}
return null;
}
Upvotes: 14
Reputation: 56727
There a file called desktop.ini
in every folder, which contains the translated folder description and icon for the folder in INI file format. Maybe you need to read that.
I found that for system folders, this references resources within system DLLs, so it may not be as simple as it seems.
That being said, you can also try the SHGetFileInfo function to obtain that information.
Just saw that Sriram Sakthivel gave a very nice answer using SHGetFileInfo
, so go for that.
Upvotes: 5