• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
猩球崛起
积小成多
博客园    首页    新随笔    联系   管理    订阅  订阅
获取可用驱动器(本地磁盘,光驱,U盘)列表

GetLogicalDrives()可以实现。具体代码:

[StructLayout(LayoutKind.Sequential)]
		public struct SHFILEINFO 
		{
			public IntPtr hIcon; 
			public int iIcon;
			public int dwAttributes; 
			public string szDisplayName; 
			public string szTypeName; 
		}

		[DllImport("shell32")]
		private static extern int SHGetFileInfo(string pszPath,int dwFileAttributes,ref SHFILEINFO psfi,int cbFileInfo,int uFlags);
		const int SHGFI_ICON = 0x0100;
		const int SHGFI_LARGEICON = 0x0000; 

		static string[] drives;
		ImageList img=new ImageList();

		private void Form1_Load(object sender, System.EventArgs e)
		{
			this.listView1.LargeImageList=img;
			this.listView1.SmallImageList=img;
			this.listView1.StateImageList=img;

			drives=Environment.GetLogicalDrives();
			for(int i=0;i<drives.Length;i++)
			{
				string str_temp=drives[i];
				this.listView1.Items.Add(str_temp);
				this.listView1.Items[i].ImageIndex=i;
			}
			for(int i=0;i<drives.Length;i++)
			{
				SHFILEINFO FileInfo=new SHFILEINFO();
				SHGetFileInfo(drives[i],0,ref FileInfo,Marshal.SizeOf(FileInfo),SHGFI_ICON | SHGFI_LARGEICON);

				Icon myIcon;
				myIcon=Icon.FromHandle(FileInfo.hIcon);
				img.Images.Add(myIcon); 
			}
		}


posted on 2013-03-10 16:25  Orangutan  阅读(252)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3