How can I get the description of a file extension in .NET

前端 未结 5 2029
囚心锁ツ
囚心锁ツ 2020-12-11 05:52

HI,

Windows provides descriptions for file extension such as \"Control Panel Item\" for .cpl files and \"PowerISO File\" for .daa files. Is there any way I can obtai

5条回答
  •  离开以前
    2020-12-11 06:30

    In order to use SHGetFileInfo the file must exist on disk. If you only have the name of the file, or even just the extension, you will need to get the information directly from the registry:

    public static string GetFileTypeDisplayName(string extension) =>
        Registry.ClassesRoot.OpenSubKey(extension)?.GetValue(null) is string keyName ?
            Registry.ClassesRoot.OpenSubKey(keyName)?.GetValue(null) as string :
            null;
    

    This method will simply return null if there is no suitable entry in the registry for the given file extension.

提交回复
热议问题