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.
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. For example, create a new VS Project called FreeSpace and paste this as Program.cs:
using System;
using System.Runtime.InteropServices;
namespace FreeSpace
{
class Program
{
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);
static void Main(string[] args)
{
ulong FreeBytesAvailable;
ulong TotalNumberOfBytes;
ulong TotalNumberOfFreeBytes;
bool success = GetDiskFreeSpaceEx(@"\\NETSHARE\folder",
out FreeBytesAvailable,
out TotalNumberOfBytes,
out TotalNumberOfFreeBytes);
if (!success)
throw new System.ComponentModel.Win32Exception();
Console.WriteLine("Free Bytes Available: {0,15:D}", FreeBytesAvailable);
Console.WriteLine("Total Number Of Bytes: {0,15:D}", TotalNumberOfBytes);
Console.WriteLine("Total Number Of FreeBytes: {0,15:D}", TotalNumberOfFreeBytes);
Console.ReadKey();
}
}
}
As you can see, this is the exact same code as in the Question linked above, just factored into a class plus the correct using
directives to compile without error. All credits go to https://stackoverflow.com/users/995926/rekire
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