Evosoul Evosoul - 1 month ago 10
C# Question

See the AvailableFreeSpace of drives via IP-Address

I know that, AvailableFreeSpace is possible to use for local drives such as "C:/", "D:/" etc.
It also works on network drives.

But now my question is:

Is it possible to view the AvailableFreeSpace of a "Folder" on another IP?
I connect to the local drives with this code:

System.IO.DriveInfo _DriveInfo = new DriveInfo(SaveLocation);


When "SaveLocation" is a local drive like "C:\Temp\Folder", than it works fine.

But when there is an IP inside "SaveLocation" it doesn't work. SaveLocation looks like this in that case: "192.168.200.10\c\Data"

This doesn't work and that is the reason for my question. The Exceptionmessage is: {"Object must be a root directory (\"C:\\") or a drive letter (\"C\")."}

I hope you can help me.

Answer

As seen in Get available disk free space for a given path on Windows :

Use the winapi function GetDiskFreeSpaceEx to determine free space on a UNC (network) path.

WMI doesn't seem to handle free space on network shares. But for local disks, Windows Management Interface is the way to go: https://msdn.microsoft.com/en-us/library/aa394592(v=vs.85).aspx

The Win32_LogicalDisk class has a Property called FreeSpace: https://msdn.microsoft.com/en-us/library/aa394173(v=vs.85).aspx#properties

The first site has VB example code that should be easy to adapt in C#. I don't have much experience with WMI, but basically you just run SQL-esque Queries which should return an Object, for which there are Types built into .NET. Here's an old code of mine to get a Win32DiskDrive (I suppose you can just replace DiskDrive with LogicalDisk)

string query = "SELECT * FROM Win32_DiskDrive";
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ArrayList localdrives = new ArrayList();

foreach (ManagementObject drive in searcher.Get())
{
    HardDrive hd = new HardDrive();
    hd.Model = drive["Model"].ToString();
}
Comments