检测磁盘是否已经准备好

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

实现效果:

  

知识运用:

  DriveInfo类的IsReady属性    //获取一个指示驱动器是否已经准备好的值

  public static bool IsReady (get;)

实现代码:

        private void Form1_Load(object sender, EventArgs e)
        {
            SelectQuery sq = new SelectQuery("Select * from Win32_logicaldisk");
            ManagementObjectSearcher searcher = new ManagementObjectSearcher(sq);
            foreach(ManagementObject m in searcher.Get())
            {
                comboBox1.Items.Add(m["Name"].ToString());
            }
            comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DriveInfo info=new DriveInfo(comboBox1.Text);
            if (info.IsReady)
                label2.Text = "该磁盘已准备好";
            else
                label2.Text = "该磁盘未准备好";
        }