获取映射驱动器路径

Posted on 2019-01-21 15:44  努力成长静待花开  阅读(404)  评论(0编辑  收藏  举报

实现效果:

  

知识运用:

  ManagementObjectSearch类和ManagementObject类

实现代码:

        private void Form1_Load(object sender, EventArgs e)
        {
            listBox1.Items.Clear();                                                       //清空列表                 
            SelectQuery selectQuery = new SelectQuery("Select * from win32_logicaldisk");   //从WMI中查询信息
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery);  //创建WMI管理对象
            foreach (ManagementObject disk in searcher.Get())                               //便利查找到的信息
            {
                string DriveType = disk["DriveType"].ToString();                            //存放存储设备类型
                if (DriveType == "4")                                                       //判断驱动器类型
                    listBox1.Items.Add(disk["Name"].ToString());                            //添加到列表中
            }
        }