简体   繁体   中英

C#: How can I retrieve the description of a folder?

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM