8

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

Community
  • 1
  • 1
yyny
  • 1,623
  • 18
  • 20

2 Answers2

14

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;
}
Community
  • 1
  • 1
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
5

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.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139