微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我怎样才能得到在.NET中的文件扩展名的描述

HI,

Windows提供文件扩展名的描述,如.cpl文件的“控制面板项目”和.daa文件的“PowerISO文件”。 有什么办法可以在.NET中获得这些数据? 即时通讯使用C#,但可以阅读所有其他的.NET语言。 还有一种方法来获得扩展的认图标? 任何帮助,将不胜感激。

提前致谢

限制.net文本框中的行数

什么是Windows平台的GPS中间驱动程序的等效物?

.NET中的“开始/关联”等效

C#WPF获取错误system.invalidOperationException:

从.NET应用程序拖到Windows资源pipe理器时强制删除作为快捷方式对象

如何在Web应用程序中设置主题

强制.NET的Windows应用程序。 以英文forms显示数字

如何检测Windows 64位的cpu速度?

处理点击ListView的一个子项

Msvcr71.dll Msvcp71.dll丢失

您可以使用SHGetFileInfo API来获取该信息。 这是一个包装方法

public static string GetFileTypeDescription(string fileNameOrExtension) { SHFILEINFO shfi; if (IntPtr.Zero != SHGetFileInfo( fileNameOrExtension,FILE_ATTRIBUTE_norMAL,out shfi,(uint)Marshal.SizeOf(typeof(SHFILEINFO)),SHGFI_USEFILEATTRIBUTES | SHGFI_TYPENAME)) { return shfi.szTypeName; } return null; } [DllImport("shell32")] private static extern IntPtr SHGetFileInfo(string pszPath,uint dwFileAttributes,out SHFILEINFO psfi,uint cbFileInfo,uint flags); [StructLayout(LayoutKind.Sequential)] private struct SHFILEINFO { public IntPtr hIcon; public int iIcon; public uint dwAttributes; [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 260)] public string szdisplayName; [MarshalAs(UnmanagedType.ByValTStr,SizeConst = 80)] public string szTypeName; } private const uint FILE_ATTRIBUTE_READONLY = 0x00000001; private const uint FILE_ATTRIBUTE_HIDDEN = 0x00000002; private const uint FILE_ATTRIBUTE_SYstem = 0x00000004; private const uint FILE_ATTRIBUTE_DIRECTORY = 0x00000010; private const uint FILE_ATTRIBUTE_ARCHIVE = 0x00000020; private const uint FILE_ATTRIBUTE_DEVICE = 0x00000040; private const uint FILE_ATTRIBUTE_norMAL = 0x00000080; private const uint FILE_ATTRIBUTE_TEMPORARY = 0x00000100; private const uint FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200; private const uint FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400; private const uint FILE_ATTRIBUTE_COMpressed = 0x00000800; private const uint FILE_ATTRIBUTE_OFFLINE = 0x00001000; private const uint FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000; private const uint FILE_ATTRIBUTE_ENCRYPTED = 0x00004000; private const uint FILE_ATTRIBUTE_VIRTUAL = 0x00010000; private const uint SHGFI_ICON = 0x000000100; // get icon private const uint SHGFI_disPLAYNAME = 0x000000200; // get display name private const uint SHGFI_TYPENAME = 0x000000400; // get type name private const uint SHGFI_ATTRIBUTES = 0x000000800; // get attributes private const uint SHGFI_ICONLOCATION = 0x000001000; // get icon location private const uint SHGFI_EXETYPE = 0x000002000; // return exe type private const uint SHGFI_SYSICONINDEX = 0x000004000; // get system icon index private const uint SHGFI_LINKOVERLAY = 0x000008000; // put a link overlay on icon private const uint SHGFI_SELECTED = 0x000010000; // show icon in selected state private const uint SHGFI_ATTR_SPECIFIED = 0x000020000; // get only specified attributes private const uint SHGFI_LARGEICON = 0x000000000; // get large icon private const uint SHGFI_SMALLICON = 0x000000001; // get small icon private const uint SHGFI_OPENICON = 0x000000002; // get open icon private const uint SHGFI_SHELLICONSIZE = 0x000000004; // get shell size icon private const uint SHGFI_PIDL = 0x000000008; // pszPath is a pidl private const uint SHGFI_USEFILEATTRIBUTES = 0x000000010; // use passed dwFileAttribute

(大部分的常量并没有在代码中实际使用,但是为了适应您的特定需求,我还是把它们放在了这里)

扩展名与HKEY_CLASSES_ROOT键相关联。 因此,您需要打开注册表并找到您感兴趣的值。您还应该能够在其中找到认图标。

这里是一个示例项目(我没有尝试过)。

可以说你的扩展名是.xyz为了显示大多数用户友好的文本,如“控制面板项目”,你应该做两件事情:

转到HKEY_CLASSES_ROOT并搜索节点xyzfile。 检查“cplfile”

如果不在那里,你可以在同一个地方得到关联,但节点是.xyz。 它会告诉你什么应用程序打开它,所以你可以得到的名字。

你可以像这样搜索注册表:

在HKEY_CLASSES_ROOT搜索扩展名的认值。 例如HKEY_CLASSES_ROOT.txt的认值是txtfile 。

然后搜索以前的结果的认值:例如HKEY_CLASSES_ROottxtfile的认值是Text Document 。

两次搜索后,答案是文本文档

您可以通过RegEdit测试任何其他扩展名。

访问此链接: http : //www.codeproject.com/KB/cs/GetFileTypeAndIcon.aspx?display=Print

这是执行这两个搜索

public static class Helper { public static string GetFileDescription(string fileName) { if (fileName == null) { throw new ArgumentNullException("fileName"); } RegistryKey registryKey1 = null; RegistryKey registryKey2 = null; try { FileInfo fileInfo = new FileInfo(fileName); if (string.IsNullOrEmpty(fileInfo.Extension)) { return string.Empty; } string extension = fileInfo.Extension.ToLowerInvariant(); registryKey1 = Registry.ClassesRoot.OpenSubKey(extension); if (registryKey1 == null) { return string.Empty; } object extensionDefaultObject = registryKey1.GetValue(null); if (!(extensionDefaultObject is string)) { return string.Empty; } string extensionDefaultValue = (string)extensionDefaultObject; registryKey2 = Registry.ClassesRoot.OpenSubKey(extensionDefaultValue); if (registryKey2 == null) { return string.Empty; } object fileDescriptionObject = registryKey2.GetValue(null); if (!(fileDescriptionObject is string)) { return string.Empty; } string fileDescription = (string)fileDescriptionObject; return fileDescription; } catch (Exception) { return null; } finally { if (registryKey2 != null) { registryKey2.Close(); } if (registryKey1 != null) { registryKey1.Close(); } } } }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐