随笔 - 81  文章 - 0  评论 - 514  阅读 - 41万

C#获取本地或远程磁盘使用信息

因为公司有多个服务器,要检查磁盘的使用情况确定程序放哪个服务器和清理垃圾,所以写个小程序帮忙检查。

效果图:

 

后台代码:

复制代码
 private void btnCheck_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            if (rbtnRemote.Checked)
            {
                //远程
                RemoteDisk();
            }
            else
            {
                //本地
                LocalDisk();
            }
        }
        //查看本地
        private void LocalDisk()
        {
            WqlObjectQuery wmiquery = new WqlObjectQuery("select * from Win32_LogiCalDisk");
            ManagementObjectSearcher wmifind = new ManagementObjectSearcher(wmiquery);
            //显示
            ShowInfo(wmifind);
        }

        //远程
        private void RemoteDisk()
        {
            if (cbIP.Text == "" | cbUserName.Text == "" | txtPwd.Text == "")
            {
                MessageBox.Show("请把信息补充完整!");
                return;
            }
            string ip = cbIP.Text;
            string username = cbUserName.Text;
            string password = txtPwd.Text;
            ConnectionOptions conn = new ConnectionOptions();
            conn.Username = username;
            conn.Password = password;
            conn.Timeout = new TimeSpan(1,1,1,1);//连接时间

            //ManagementScope 的服务器和命名空间。  
            string path = string.Format(@"\\{0}\root\cimv2", ip);
            //表示管理操作的范围(命名空间),使用指定选项初始化ManagementScope 类的、表示指定范围路径的新实例。 
            ManagementScope scope = new ManagementScope(path, conn);  
            scope.Connect(); 
            //查询
            string strQuery = "select * from Win32_LogicalDisk ";
            ObjectQuery query = new ObjectQuery(strQuery);
            //查询ManagementObjectCollection返回结果集  
            ManagementObjectSearcher wmifind = new ManagementObjectSearcher(scope, query);
            ShowInfo(wmifind);
        }

        #region 显示磁盘信息
        private void ShowInfo(ManagementObjectSearcher wmifind)
        {
            long gb = 1024 * 1024 * 1024;
            string type = "";
            string str = "";
            double freePath = 0d;
            foreach (var mobj in wmifind.Get())
            {
                type = mobj["Description"].ToString();
                //判断是否是本机固盘
                if (type == "Local Fixed Disk")
                {
                    str = " 磁盘名称:" + mobj["Name"].ToString();
                    freePath = Math.Round(Convert.ToDouble(mobj["FreeSpace"]) / gb, 1);
                    str += " 可用空间:" + freePath+ "G";
                    str += " 实际大小:" + Math.Round(Convert.ToDouble(mobj["Size"].ToString()) / gb, 1) + "G";
                    if (freePath < 20)
                    {
                        str += "    ----请尽快清理!";
                    }
                    listBox1.Items.Add(str);
                }
            }
        }
        #endregion

        private void rbtnLocal_CheckedChanged(object sender, EventArgs e)
        {
            //本地选中
            if (rbtnLocal.Checked == true)
            {
                cbIP.Enabled = false;
                cbUserName.Enabled = false;
                txtPwd.Enabled = false;
            }
        }

        private void rbtnRemote_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnRemote.Checked == true)
            {
                cbIP.Enabled = true;
                cbUserName.Enabled = true;
                txtPwd.Enabled = true;
            }
        }
复制代码

 

posted on   包子wxl  阅读(2361)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏