如何检测可用空间和磁盘信息从一台服务器到另一个在C#一台、磁盘、服务器、空间

2023-09-03 04:31:21 作者:影子爱人~

如果有3个网络,如果我想从另一台PC检测到一台PC的可用空间和磁盘的详细信息,然后如何去了解它.... 我发现这个code。但我不knw我应该如何测试序知道它是否工作.. 这是正确的方式。

 公共的Hashtable ReadFreeSpaceOnNetworkDrives()
{
     //创建Hashtable的实例来保存我们的信息
     Hashtable的driveInfo =新的Hashtable();
     //查询的Win32_LogicalDisk为4类(网盘)
     SelectQuery查询=新SelectQuery(距离的Win32_LogicalDisk选择名称,可用空间在哪里DRIVETYPE = 4);

     //执行使用WMI查询
     ManagementObjectSearcher搜索=新ManagementObjectSearcher(查询);
     //通过每个驱动器环路发现
     的foreach(在searcher.Get的ManagementObject驱动器())
     {
        //添加名称和放大器;自由空间给我们的哈希表
        driveInfo.Add(驱动器,驱动器[名称]);
        driveInfo.Add(空间,推动[自由空间]);
     }
     返回driveInfo;
}
 

更新:

我得到的回答我的问题,但我已经拿到了code,但它在控制台应用程序,我想与磁盘空间的图形重新presentation Windows窗体应用程序和驱动器的信息。我如何使用这个code和去这样做?

 管理​​范围范围=新的管理范围(\\\\ 10.74.160.126 \\ \\根CIMV2);
        scope.Connect();

        的ObjectQuery查询=新的ObjectQuery(SELECT * FROM的Win32_OperatingSystem);
        SelectQuery QUERY1 =新SelectQuery(SELECT * FROM的Win32_LogicalDisk);

        ManagementObjectSearcher搜索=新ManagementObjectSearcher(范围查询);

        ManagementObjectCollection queryCollection = searcher.Get();

        ManagementObjectSearcher searcher1 =新ManagementObjectSearcher(范围,QUERY1);
        ManagementObjectCollection queryCollection1 = searcher1.Get();

        的foreach(在queryCollection的ManagementObject米)
        {
            //显示远程计算机的信息
            Console.WriteLine(计算机名称:{0},
                M [csname]);
            Console.WriteLine(Windows目录:{0},
                M [WindowsDirectory]);
            Console.WriteLine(操作系统:{0},
                M [标题]);
            Console.WriteLine(版本:{0},M [版本]);
            Console.WriteLine(制造商:{0},M [制造商]);
            Console.WriteLine();

            }

        的foreach(的ManagementObject莫queryCollection1)
        {
            Console.WriteLine(磁盘名称:{0},莫[名称]);
            Console.WriteLine(磁盘大小:{0},莫[大小]);
            Console.WriteLine(自由空间:{0},莫[自由空间]);
            Console.WriteLine(磁盘的DeviceID:{0},莫[的DeviceID]);
            Console.WriteLine(磁盘卷名:{0},莫[卷名]);
            Console.WriteLine(磁盘的系统名称:{0},莫[的SystemName]);
            Console.WriteLine(磁盘VolumeSerialNumber:{0},莫[VolumeSerialNumber]);
            Console.WriteLine();
        }
        到Console.ReadLine();
        }
 
如何在 Mac 上检查磁盘空间使用情况,三个方法轻松解决

解决方案

在code盘你在哪里运行此程序在PC的所有驱动器。它返回每个驱动器2项的表。一个名为,一个与自由空间。你可以只写一个使用此方法,并显示这些数据的简单程序。它应该可以从远程计算机查询驱动器。也许这篇文章可以告诉你更多的http:// msdn.microsoft.com/en-us/library/ms257337%28v=vs.80%29.aspx

编辑:

 公共的Hashtable ReadFreeSpaceOnNetworkDrives(字符串FullComputerName)
{
     管理范围范围=新的管理范围(fullComputerName);
     scope.Connect();
     //创建Hashtable的实例来保存我们的信息
     Hashtable的driveInfo =新的Hashtable();
     //查询的Win32_LogicalDisk为4类(网盘)
     SelectQuery查询=新SelectQuery(距离的Win32_LogicalDisk选择名称,可用空间在哪里DRIVETYPE = 4);

     //执行使用WMI查询
     ManagementObjectSearcher搜索=新ManagementObjectSearcher(范围查询);
     //通过每个驱动器环路发现
     的foreach(在searcher.Get的ManagementObject驱动器())
     {
        //添加名称和放大器;自由空间给我们的哈希表
        driveInfo.Add(驱动器,驱动器[名称]);
        driveInfo.Add(空间,推动[自由空间]);
     }
     返回driveInfo;
}
 

If there are 3 pcs on network and if i want to detect the freespace and disk details of one pc from another pc then how to go about it.... I have found this code. but i dont knw how should i test it inorder to know whether it is working.. is this the right way..

public Hashtable ReadFreeSpaceOnNetworkDrives()
{
     //create Hashtable instance to hold our info
     Hashtable driveInfo = new Hashtable();
     //query the win32_logicaldisk for type 4 (Network drive)
     SelectQuery query = new SelectQuery("select name, FreeSpace from win32_logicaldisk where drivetype=4");

     //execute the query using WMI
     ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
     //loop through each drive found
     foreach (ManagementObject drive in searcher.Get())
     {
        //add the name & freespace to our hashtable
        driveInfo.Add("Drive", drive["name"]);
        driveInfo.Add("Space", drive["FreeSpace"]);
     }
     return driveInfo;
}

Update:

I got the answer to my question but I have got the code but it is in console application and I want in windows form application with a graphical representation of the disk space and drive info. How can I use this code and go about doing that?

        ManagementScope scope = new ManagementScope("\\\\10.74.160.126\\root\\cimv2");
        scope.Connect();

        ObjectQuery query = new ObjectQuery( "SELECT * FROM Win32_OperatingSystem");
        SelectQuery query1 = new SelectQuery("Select * from Win32_LogicalDisk");

        ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

        ManagementObjectCollection queryCollection = searcher.Get();

        ManagementObjectSearcher searcher1 = new ManagementObjectSearcher(scope, query1);
        ManagementObjectCollection queryCollection1 = searcher1.Get(); 

        foreach (ManagementObject m in queryCollection)
        {
            // Display the remote computer information
            Console.WriteLine("Computer Name : {0}",
                m["csname"]);
            Console.WriteLine("Windows Directory : {0}",
                m["WindowsDirectory"]);
            Console.WriteLine("Operating System: {0}",
                m["Caption"]);
            Console.WriteLine("Version: {0}", m["Version"]);
            Console.WriteLine("Manufacturer : {0}", m["Manufacturer"]);
            Console.WriteLine();

            }

        foreach (ManagementObject mo in queryCollection1)
        {
            Console.WriteLine("  Disk Name : {0}", mo["Name"]);
            Console.WriteLine("   Disk Size : {0}", mo["Size"]); 
            Console.WriteLine("  FreeSpace : {0}", mo["FreeSpace"]);
            Console.WriteLine("  Disk DeviceID : {0}", mo["DeviceID"]); 
            Console.WriteLine("  Disk VolumeName : {0}", mo["VolumeName"]);
            Console.WriteLine("  Disk SystemName : {0}", mo["SystemName"]); 
            Console.WriteLine("Disk VolumeSerialNumber : {0}", mo["VolumeSerialNumber"]);
            Console.WriteLine(); 
        }
        Console.ReadLine();
        }

解决方案

The code checks all the drives on the pc where you run this program. It returns a table with 2 entries per drive. One with the name and one with the free space. You can just write a simple program that uses this method and displays this data. It should be possible to query the drives from a remote computer. Maybe this article can tell you more http://msdn.microsoft.com/en-us/library/ms257337%28v=vs.80%29.aspx

EDIT:

public Hashtable ReadFreeSpaceOnNetworkDrives(String FullComputerName)
{
     ManagementScope scope = new ManagementScope(fullComputerName);
     scope.Connect();
     //create Hashtable instance to hold our info
     Hashtable driveInfo = new Hashtable();
     //query the win32_logicaldisk for type 4 (Network drive)
     SelectQuery query = new SelectQuery("select name, FreeSpace from win32_logicaldisk where drivetype=4");

     //execute the query using WMI
     ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope,query);
     //loop through each drive found
     foreach (ManagementObject drive in searcher.Get())
     {
        //add the name & freespace to our hashtable
        driveInfo.Add("Drive", drive["name"]);
        driveInfo.Add("Space", drive["FreeSpace"]);
     }
     return driveInfo;
}