当前位置:编程学习 > C#/ASP.NET >>

asp中如何显示磁盘及文件图标



例如如下效果:不使用外部图片

--------------------编程问答-------------------- 帮顶 --------------------编程问答-------------------- 呵呵 不使用外部图片可以使用系统图标不..这个黑白的画出来太麻烦了. --------------------编程问答-------------------- 记得我也做过一个类似的东西,就是列出本机的文件,跟“我的电脑”中的是一样的,
我当时是用图片(省事,省时),HTML的<img src = ""></img>  LZ有别的办法的发分享下啊 --------------------编程问答--------------------

using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Collections;
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.Text;
using System.Data.SqlClient;
using System.Xml;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            System.IO.DriveInfo[] _DriveList = System.IO.DriveInfo.GetDrives();
            int _SizeWidth = 80;
            Bitmap _Bitmap = new Bitmap(_DriveList.Length * _SizeWidth, _SizeWidth);
            
            Graphics _Graphcis = Graphics.FromImage(_Bitmap);
            _Graphcis.Clear(Color.White);
            for (int i = 0; i != _DriveList.Length; i++)
            {
                _Graphcis.DrawImage(GetFileIcon(_DriveList[i].Name).ToBitmap(), _SizeWidth * i + ((_SizeWidth - 32) / 2), 0);

                StringFormat _Format = new StringFormat();
                _Format.Alignment = StringAlignment.Center;
                _Format.LineAlignment = StringAlignment.Center;

                switch(_DriveList[i].DriveType)
                {
                    case System.IO.DriveType.Fixed:
                        _Graphcis.DrawString("本地磁盘", new System.Drawing.Font("宋体", 10), Brushes.Black, new Rectangle(_SizeWidth * i, 32, _SizeWidth, _SizeWidth - 32), _Format);
                        break;
                    case System.IO.DriveType.CDRom:
                        _Graphcis.DrawString("CD 驱动器", new System.Drawing.Font("宋体", 10), Brushes.Black, new Rectangle(_SizeWidth * i, 32, _SizeWidth, _SizeWidth - 32), _Format);
                        break;
                }

                _Graphcis.DrawString(_DriveList[i].Name.Replace("\\", ""), new System.Drawing.Font("宋体", 10), Brushes.Black, new Rectangle(_SizeWidth * i, 52, _SizeWidth, _SizeWidth - 52), _Format);
            

                
            }
            _Graphcis.Dispose();

            this.Response.Clear();
            
            _Bitmap.Save(this.Response.OutputStream, ImageFormat.Jpeg);
            _Bitmap.Dispose();
            this.Response.End();
        }
    }

    [StructLayout(LayoutKind.Sequential)]   
    public 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;   
    }   
  
    /// <summary>   
    /// 返回系统设置的图标   
    /// </summary>   
    /// <param name="pszPath">文件路径 如果为""  返回文件夹的</param>   
    /// <param name="dwFileAttributes">0</param>   
    /// <param name="psfi">结构体</param>   
    /// <param name="cbSizeFileInfo">结构体大小</param>   
    /// <param name="uFlags">枚举类型</param>   
    /// <returns>-1失败</returns>   
    [DllImport("shell32.dll")]   
    public static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref   SHFILEINFO psfi, uint cbSizeFileInfo, uint uFlags);   
  
    public enum SHGFI   
    {   
        SHGFI_ICON = 0x100,   
        SHGFI_LARGEICON = 0x0,   
        SHGFI_USEFILEATTRIBUTES = 0x10   
    }   
  
  
    /// <summary>   
    /// 获取文件图标 zgke@sina.com qq:116149   
    /// </summary>   
    /// <param name="p_Path">文件全路径</param>   
    /// <returns>图标</returns>   
    public static Icon GetFileIcon(string p_Path)   
    {   
        SHFILEINFO _SHFILEINFO = new SHFILEINFO();   
        IntPtr _IconIntPtr = SHGetFileInfo(p_Path, 0, ref _SHFILEINFO, (uint)Marshal.SizeOf(_SHFILEINFO), (uint)(SHGFI.SHGFI_ICON | SHGFI.SHGFI_LARGEICON | SHGFI.SHGFI_USEFILEATTRIBUTES));   
        if (_IconIntPtr.Equals(IntPtr.Zero)) return null;   
        Icon _Icon = System.Drawing.Icon.FromHandle(_SHFILEINFO.hIcon);   
        return _Icon;   
    }   
  

}




看看这个效果.
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,