Getting disk information from a remote computer

I can view the remotely connected computer from this article. : Remote desktop using c-net . but I don’t need it. I just need to connect to this computer and get the free space data on drive C. How can I do this? I can connect to remote desktop. I can get driveInfo using the IO namespace. but how to combine them?

+4
source share
3 answers

Use the System.Management namespace and the Win32_Volume WMI class to do this. You can request an instance with DriveLetter from C: and get its FreeSpace property as follows:

 ManagementPath path = new ManagementPath() { NamespacePath = @"root\cimv2", Server = "<REMOTE HOST OR IP>" }; ManagementScope scope = new ManagementScope(path); string condition = "DriveLetter = 'C:'"; string[] selectedProperties = new string[] { "FreeSpace" }; SelectQuery query = new SelectQuery("Win32_Volume", condition, selectedProperties); using (ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query)) using (ManagementObjectCollection results = searcher.Get()) { ManagementObject volume = results.Cast<ManagementObject>().SingleOrDefault(); if (volume != null) { ulong freeSpace = (ulong) volume.GetPropertyValue("FreeSpace"); // Use freeSpace here... } } 

There is also a Capacity property that stores the total volume size.

+15
source

Here is the equivalent of vb.net if you need to translate it.

  Dim path = New ManagementPath With {.NamespacePath = "root\cimv2", .Server = "<REMOTE HOST OR IP>"} Dim scope = New ManagementScope(path) Dim condition = "DriveLetter = 'C:'" Dim selectedProperties = {"FreeSpace"} Dim query = New SelectQuery("Win32_Volume", condition, selectedProperties) Dim searcher = New ManagementObjectSearcher(scope, query) Dim results = searcher.Get() Dim volume = results.Cast(Of ManagementObject).SingleOrDefault() If volume IsNot Nothing Then Dim freeSpace As ULong = volume.GetPropertyValue("FreeSpace") End If 
+1
source

All Articles