如题!谢谢
解决方案 »
- 【online】定义变量时,都定义成 var 格式,好还是不好啊?
- 关于repeater 嵌套 ul onmouseout不执行高手指点 在线等候
- 小弟初学关于多线程“锁”的问题(附源码)
- 关于页面、用户控件排版问题?
- 有关权限的问题各位都是怎么解决的?
- 请大家看看我这个触发器哪有错误,找不出来我
- 請幫幫我,哪位用過wise installer v6.00的啊??我怎麼運行的時候出現一個odbc數據源配置啊?
- ASP.net程序,同一个模板,在不同的电脑上打印预览不统一,怎么办?
- 如何判断是空值
- 一个简单的ASP数据显示问题
- web中即时交流如何实现
- 网页下载文件,急!!!!!好心人路过帮下忙
case "txt":
currentTreeNode.ImageUrl = "images/ext_txt.gif";
break;
case "gif":
currentTreeNode.ImageUrl = "images/ext_gif.gif";
break;
...
}
http://files.cnblogs.com/tengs2000/IconUtils.zip
using System.Runtime.InteropServices;
using System.Drawing;namespace MyNamespace
{
public class FileIcon
{
/// <summary>
/// 获取文件的默认图标
/// </summary>
/// <param name="fileName">文件名。
/// 可以只是文件名,甚至只是文件的扩展名(.*);
/// 如果想获得.ICO文件所表示的图标,则必须是文件的完整路径。
/// </param>
/// <param name="largeIcon">是否大图标</param>
/// <returns>文件的默认图标</returns>
public static Icon GetFileIcon(string fileName, bool largeIcon)
{
SHFILEINFO info = new SHFILEINFO(true);
int cbFileInfo = Marshal.SizeOf(info);
SHGFI flags;
if (largeIcon)
flags = SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes;
else
flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes; SHGetFileInfo(fileName, 256, out info, (uint) cbFileInfo, flags);
return Icon.FromHandle(info.hIcon);
}
[DllImport("Shell32.dll")]
private static extern int SHGetFileInfo
(
string pszPath,
uint dwFileAttributes,
out SHFILEINFO psfi,
uint cbfileInfo,
SHGFI uFlags
); [StructLayout(LayoutKind.Sequential)]
private struct SHFILEINFO
{
public SHFILEINFO(bool b)
{
hIcon = IntPtr.Zero;
iIcon = 0;
dwAttributes = 0;
szDisplayName = "";
szTypeName = "";
}
public IntPtr hIcon;
public int iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.LPStr, SizeConst = 80)]
public string szTypeName;
}; private enum SHGFI
{
SmallIcon = 0x00000001,
LargeIcon = 0x00000000,
Icon = 0x00000100,
DisplayName = 0x00000200,
Typename = 0x00000400,
SysIconIndex = 0x00004000,
UseFileAttributes = 0x00000010
}
}
}
以上代码可以实现你要的功能,不过嘛,遍历并呈现在web上并不是啥好方法,在web上应该尽量使用轻量级的东西,为了一个无伤大雅的小功能而无端的耗时间不划算啊
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
const uint SHGFI_ICON = 0x100;
const uint SHGFI_USEFILEATTRIBUTES = 0x10; [DllImport("Shell32", CharSet = CharSet.Auto)]
internal extern static int ExtractIconEx(
[MarshalAs(UnmanagedType.LPTStr)]
string lpszFile, //size of the icon
int nIconIndex, //index of the icon
//(in case we have more
//then 1 icon in the file
IntPtr[] phIconLarge, //32x32 icon
IntPtr[] phIconSmall, //16x16 icon
int nIcons); //how many to get [DllImport("shell32.dll")]
static extern IntPtr SHGetFileInfo(
string pszPath, //path
uint dwFileAttributes, //attributes
ref SHFILEINFO psfi, //struct pointer
uint cbSizeFileInfo, //size
uint uFlags); //flags //we need this function to release the unmanaged resource,
//the unmanaged resource will be
//copies to a managed one and it will be returned.
[DllImport("user32.dll", CharSet = CharSet.Auto)]
extern static bool DestroyIcon(IntPtr handle);
struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
public enum IconSize : uint
{
Large = 0x0, //32x32
Small = 0x1 //16x16
}
public static Icon IconFromExtension(string Extension,
IconSize Size)
{
try
{
Icon TempIcon; //add '.' if nessesry
if (Extension[0] != '.')
Extension = '.' + Extension; //temp struct for getting file shell info
SHFILEINFO TempFileInfo = new SHFILEINFO(); SHGetFileInfo(
Extension,
0,
ref TempFileInfo,
(uint)Marshal.SizeOf(TempFileInfo),
SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | (uint)Size); TempIcon = (Icon)Icon.FromHandle(TempFileInfo.hIcon);
return GetManagedIcon(ref TempIcon);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("error" +
" while trying to get icon for " +
Extension + " :" + e.Message);
return null;
}
}
public static Icon GetManagedIcon(ref Icon UnmanagedIcon)
{
Icon ManagedIcon = (Icon)UnmanagedIcon.Clone(); DestroyIcon(UnmanagedIcon.Handle); return ManagedIcon;
}
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
System.Drawing.Icon icon = IconFromExtension("txt", IconSize.Large);
this.Icon = icon;
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Runtime.InteropServices;public partial class _Default : System.Web.UI.Page
{
const uint SHGFI_ICON = 0x100;
const uint SHGFI_USEFILEATTRIBUTES = 0x10; [DllImport("Shell32", CharSet = CharSet.Auto)]
internal extern static int ExtractIconEx(
[MarshalAs(UnmanagedType.LPTStr)]
string lpszFile, //size of the icon
int nIconIndex, //index of the icon
//(in case we have more
//then 1 icon in the file
IntPtr[] phIconLarge, //32x32 icon
IntPtr[] phIconSmall, //16x16 icon
int nIcons); //how many to get [DllImport("shell32.dll")]
static extern IntPtr SHGetFileInfo(
string pszPath, //path
uint dwFileAttributes, //attributes
ref SHFILEINFO psfi, //struct pointer
uint cbSizeFileInfo, //size
uint uFlags); //flags //we need this function to release the unmanaged resource,
//the unmanaged resource will be
//copies to a managed one and it will be returned.
[DllImport("user32.dll", CharSet = CharSet.Auto)]
extern static bool DestroyIcon(IntPtr handle);
struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
public enum IconSize : uint
{
Large = 0x0, //32x32
Small = 0x1 //16x16
}
public static Icon IconFromExtension(string Extension,
IconSize Size)
{
try
{
Icon TempIcon; //add '.' if nessesry
if (Extension[0] != '.')
Extension = '.' + Extension; //temp struct for getting file shell info
SHFILEINFO TempFileInfo = new SHFILEINFO(); SHGetFileInfo(
Extension,
0,
ref TempFileInfo,
(uint)Marshal.SizeOf(TempFileInfo),
SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | (uint)Size); TempIcon = (Icon)Icon.FromHandle(TempFileInfo.hIcon);
return GetManagedIcon(ref TempIcon);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("error" +
" while trying to get icon for " +
Extension + " :" + e.Message);
return null;
}
}
public static Icon GetManagedIcon(ref Icon UnmanagedIcon)
{
Icon ManagedIcon = (Icon)UnmanagedIcon.Clone(); DestroyIcon(UnmanagedIcon.Handle); return ManagedIcon;
} protected void Page_Load(object sender, EventArgs e)
{
Icon icon = IconFromExtension("rar", IconSize.Large);
using(System.IO.MemoryStream ms=new System.IO.MemoryStream())
{
icon.ToBitmap().Save(this.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
ms.Close();
}
}
}