用于计算 Windows Server 2008 上安装点上的可用空间和驱动器空间的 C# WMI 代码

发布于 2024-09-24 04:31:04 字数 88 浏览 3 评论 0原文

我找到了大量有关如何查看物理驱动器的信息,但我需要监视物理驱动器上的多个安装点。我更愿意通过 wmi 来完成此操作,但任何可以完成此操作的 .NET 对象也可以。

I have found plenty of information on how to look at physical drives but I need to monitor several mount points on my physical drives. I would prefer to do this through wmi but any .NET objects that could do it would be fine as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

琉璃繁缕 2024-10-01 04:31:04

所以我终于弄清楚了这一点。对于 WMI,它是一场与糟糕文档的持续斗争,并且需要您自己解决这些问题。

资源

用于测试查询的 wbemtest

wbemtest 参考

用于生成多种语言的 wmi 脚本的脚本

scriptomatic

代码:

using System;
using System.IO;
using System.Management;
using System.Diagnostics;


namespace Monitor
{
    class Program
    {
        static void Main(string[] args)
        {

            double Free, Size, FreePercentage;
            DateTime Now = DateTime.Now;

            string scopeStr = string.Format(@"root\cimv2", "TestSqlServer");


            ManagementScope scope = new ManagementScope(scopeStr);
            scope.Connect();

            string queryString = "SELECT * FROM Win32_Volume WHERE DriveLetter IS NULL";
            SelectQuery query = new SelectQuery(queryString);
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
            {
                Console.WriteLine("Entering Volume loop: ");
                foreach (ManagementObject disk in searcher.Get())
                {
                    Console.WriteLine("foreach Volume: ");
                    //-------------------------------------------------------------------------
                    //Console.WriteLine("Free %" + double.Parse(disk["FreeSpace"].ToString()) / double.Parse(disk["Capacity"].ToString()) * 100);
                    FreePercentage = double.Parse(disk["FreeSpace"].ToString()) / double.Parse(disk["Capacity"].ToString()) * 100;
                    //-------------------------------------------------------------------------
                    string _MountPoint = disk["Name"].ToString();
                    //Console.WriteLine("Free: " + disk["FreeSpace"].ToString());
                    Free = Math.Round(Convert.ToDouble(disk["FreeSpace"]) / (1024 * 1024), 2);
                    Console.WriteLine("Free: " + Free + " MB");
                    //Console.WriteLine("Capacity: " + disk["Capacity"].ToString());
                    Size = Math.Round(Convert.ToDouble(disk["Capacity"]) / (1024 * 1024), 2);
                    Console.WriteLine("Size: " + Size + " MB");
                    if (_MountPoint[_MountPoint.Length - 1] == Path.DirectorySeparatorChar)
                    {
                        _MountPoint = _MountPoint.Remove(_MountPoint.Length - 1);
                    }
                    _MountPoint = _MountPoint.Replace("\\", "\\\\\\\\");

                    string _MountPointQueryString = "select * FROM Win32_MountPoint WHERE Directory=\"Win32_Directory.Name=\\\"" + _MountPoint + "\\\"\"";

                    SelectQuery _MountPointQuery = new SelectQuery(_MountPointQueryString);
                    using (
                        ManagementObjectSearcher mpsearcher =
                            new ManagementObjectSearcher(scope, _MountPointQuery))
                    {
                        Console.WriteLine("Entering directory Foreach loop: ");
                        foreach (ManagementObject mp in mpsearcher.Get())
                        {
                            Console.WriteLine("Foreach directory: ");

                            try
                            {
                                //Console.WriteLine("Volume: " + mp["Volume"].ToString());
                                Console.WriteLine("Directory: " + mp["Directory"].ToString());
                                string Volume = mp["Directory"].ToString().Replace("Win32_Directory.Name=", "");


                                if (FreePercentage <= 5.00)
                                {
                                    throw new Exception("\nLabel: " + Volume + "\nSeverity: " + EventLogEntryType.Error + "\nTime: " + Now + "\nMessage: disk space threshhold: " + CalculateUsedSpace(Free, Size) + " % used (" + Free + "MB" + " free)");
                                }

                            }
                            catch (Exception ex)
                            {
                                EventLog.WriteEntry("DriveStats Warning", "Message: " + ex.Message, EventLogEntryType.Error);
                            }
                        }
                    }
                }
            }


        }

        static double CalculateUsedSpace(double f, double s)
        {
            double UsedPercentage;
            if (s == 0)
            {
                return f;
            }
            else if (s >= 0 && f == 0)
            {
                UsedPercentage = 100.00;
                return UsedPercentage;
            }
            else
            {
                double UsedSpace = s - f;
                UsedPercentage = (UsedSpace / s) * 100;
                UsedPercentage = Math.Round(UsedPercentage, 2);
                //Console.WriteLine("Used Percentage: " + UsedPercentage);
                return Math.Round(UsedPercentage, 2);
            }
        }

        static double CalculateFreePercentage(double f, double s)
        {
            double FreePercentage;
            if (f == 0)
            {
                FreePercentage = 0;
                return FreePercentage;
            }
            else
            {
                FreePercentage = (f / s) * 100;
                //Console.WriteLine("Free Percentage: " + FreePercentage);
                return Math.Round(FreePercentage, 2);
            }

        }
    }
}

So I figured this one out finally. With WMI its a constant battle against poor documentation and just figuring out this stuff on your own.

RESOURCES

wbemtest for testing your queries

wbemtest reference

scriptomatic for generating wmi scipts in several languages

scriptomatic

The code:

using System;
using System.IO;
using System.Management;
using System.Diagnostics;


namespace Monitor
{
    class Program
    {
        static void Main(string[] args)
        {

            double Free, Size, FreePercentage;
            DateTime Now = DateTime.Now;

            string scopeStr = string.Format(@"root\cimv2", "TestSqlServer");


            ManagementScope scope = new ManagementScope(scopeStr);
            scope.Connect();

            string queryString = "SELECT * FROM Win32_Volume WHERE DriveLetter IS NULL";
            SelectQuery query = new SelectQuery(queryString);
            using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query))
            {
                Console.WriteLine("Entering Volume loop: ");
                foreach (ManagementObject disk in searcher.Get())
                {
                    Console.WriteLine("foreach Volume: ");
                    //-------------------------------------------------------------------------
                    //Console.WriteLine("Free %" + double.Parse(disk["FreeSpace"].ToString()) / double.Parse(disk["Capacity"].ToString()) * 100);
                    FreePercentage = double.Parse(disk["FreeSpace"].ToString()) / double.Parse(disk["Capacity"].ToString()) * 100;
                    //-------------------------------------------------------------------------
                    string _MountPoint = disk["Name"].ToString();
                    //Console.WriteLine("Free: " + disk["FreeSpace"].ToString());
                    Free = Math.Round(Convert.ToDouble(disk["FreeSpace"]) / (1024 * 1024), 2);
                    Console.WriteLine("Free: " + Free + " MB");
                    //Console.WriteLine("Capacity: " + disk["Capacity"].ToString());
                    Size = Math.Round(Convert.ToDouble(disk["Capacity"]) / (1024 * 1024), 2);
                    Console.WriteLine("Size: " + Size + " MB");
                    if (_MountPoint[_MountPoint.Length - 1] == Path.DirectorySeparatorChar)
                    {
                        _MountPoint = _MountPoint.Remove(_MountPoint.Length - 1);
                    }
                    _MountPoint = _MountPoint.Replace("\\", "\\\\\\\\");

                    string _MountPointQueryString = "select * FROM Win32_MountPoint WHERE Directory=\"Win32_Directory.Name=\\\"" + _MountPoint + "\\\"\"";

                    SelectQuery _MountPointQuery = new SelectQuery(_MountPointQueryString);
                    using (
                        ManagementObjectSearcher mpsearcher =
                            new ManagementObjectSearcher(scope, _MountPointQuery))
                    {
                        Console.WriteLine("Entering directory Foreach loop: ");
                        foreach (ManagementObject mp in mpsearcher.Get())
                        {
                            Console.WriteLine("Foreach directory: ");

                            try
                            {
                                //Console.WriteLine("Volume: " + mp["Volume"].ToString());
                                Console.WriteLine("Directory: " + mp["Directory"].ToString());
                                string Volume = mp["Directory"].ToString().Replace("Win32_Directory.Name=", "");


                                if (FreePercentage <= 5.00)
                                {
                                    throw new Exception("\nLabel: " + Volume + "\nSeverity: " + EventLogEntryType.Error + "\nTime: " + Now + "\nMessage: disk space threshhold: " + CalculateUsedSpace(Free, Size) + " % used (" + Free + "MB" + " free)");
                                }

                            }
                            catch (Exception ex)
                            {
                                EventLog.WriteEntry("DriveStats Warning", "Message: " + ex.Message, EventLogEntryType.Error);
                            }
                        }
                    }
                }
            }


        }

        static double CalculateUsedSpace(double f, double s)
        {
            double UsedPercentage;
            if (s == 0)
            {
                return f;
            }
            else if (s >= 0 && f == 0)
            {
                UsedPercentage = 100.00;
                return UsedPercentage;
            }
            else
            {
                double UsedSpace = s - f;
                UsedPercentage = (UsedSpace / s) * 100;
                UsedPercentage = Math.Round(UsedPercentage, 2);
                //Console.WriteLine("Used Percentage: " + UsedPercentage);
                return Math.Round(UsedPercentage, 2);
            }
        }

        static double CalculateFreePercentage(double f, double s)
        {
            double FreePercentage;
            if (f == 0)
            {
                FreePercentage = 0;
                return FreePercentage;
            }
            else
            {
                FreePercentage = (f / s) * 100;
                //Console.WriteLine("Free Percentage: " + FreePercentage);
                return Math.Round(FreePercentage, 2);
            }

        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文