日期:2014-05-18  浏览次数:20988 次

如何获取listview列表中磁盘驱动器的名字
winform环境,listview列表中显示出磁盘驱动器,例如,C,D,E盘及图标等,现在想通过双击直接获取盘符或者对应磁盘的名字,应该在listview的DoubleClick里面添写哪些代码,谢谢!

------解决方案--------------------
C# code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            DriveInfo[] dr = DriveInfo.GetDrives();
            foreach (DriveInfo dd in dr)
            {
                if (dd.DriveType == DriveType.CDRom)  //过滤掉是光驱的 磁盘
                {
                    return;
                }
                else
                {
                    //listBox1.Items.Add(dd);
                    ListViewItem li = new ListViewItem();
                    li.SubItems[0].Text = dd.ToString();
                    li.SubItems.Add(dd.ToString());

                    listView1.Items.Add(li);
                }

            }  

        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listView1.SelectedIndices != null && listView1.SelectedIndices.Count > 0)
            {
                ListView.SelectedIndexCollection c = listView1.SelectedIndices;
                MessageBox.Show(listView1.Items[c[0]].Text);
            }

        }
    }
}