开发者

How to detect freespace and disk information from one server to other in C#

开发者 https://www.devze.com 2023-02-18 12:52 出处:网络
If there are 3 pcs on network and if I want to detect the freespace and disk details of one pcfrom anotherpc then how to go about it...

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 don't know 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;
}


covert into c#. and that may help you. http://www.codeguru.com/forum/showthread.php?t=426869

0

精彩评论

暂无评论...
验证码 换一张
取 消