根据后缀名得到的,参考
全局代码
<System.Runtime.InteropServices.DllImportAttribute("shell32.dll", EntryPoint := "ExtractIconExW", CallingConvention := System.Runtime.InteropServices.CallingConvention.StdCall)> _
Public Function ExtractIconExW(<System.Runtime.InteropServices.InAttribute> <System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.LPWStr)> lpszFile As String, nIconIndex As Integer, ByRef phiconLarge As System.IntPtr, ByRef phiconSmall As System.IntPtr, nIcons As UInteger) As UInteger
End Function
------------
获取代码
Dim extsubkey = Registry.ClassesRoot.OpenSubKey(".xls")
'从注册表中读取扩展名相应的子键
If extsubkey IsNot Nothing Then
Dim extdefaultvalue = DirectCast(extsubkey.GetValue(Nothing), String)
'取出扩展名对应的文件类型名称
Dim typesubkey = Registry.ClassesRoot.OpenSubKey(extdefaultvalue)
'从注册表中读取文件类型名称的相应子键
If typesubkey IsNot Nothing Then
Dim description = DirectCast(typesubkey.GetValue(Nothing), String)
'得到类型描述字符串
Dim defaulticonsubkey = typesubkey.OpenSubKey("DefaultIcon")
'取默认图标子键
If defaulticonsubkey IsNot Nothing Then
'得到图标来源字符串
Dim defaulticon = DirectCast(defaulticonsubkey.GetValue(Nothing), String)
'取出默认图标来源字符串
Dim iconstringArray = defaulticon.Split(","C)
Dim nIconIndex As Integer = 0
If iconstringArray.Length > 1 Then
Integer.TryParse(iconstringArray(1), nIconIndex)
End If
'得到图标
Dim phiconLarge As new System.IntPtr
Dim phiconSmall As new System.IntPtr
ExtractIconExW(iconstringArray(0).Trim(""""C), nIconIndex, phiconLarge, phiconSmall, 1)
Dim icon As icon = Icon.FromHandle(phiconLarge)
Dim fileStream As new System.IO.FileStream("d:\test.ico", System.IO.FileMode.Create)
icon.Save(fileStream)
fileStream.Close()
End If
End If
End If
[此贴子已经被作者于2014-8-28 17:00:04编辑过]