C# – Get free space of network harddisk

Although working as a software developer but I am interested in Networking too. Yesterday when I was “running around” the file servers of my company, I found an interesting question but I can not answer myself therefore I would like to post my question here so that if someone knows the answer can help me. In my company there is a server whose name is “srvxxxx” and publicizes theses folders to all network users

Folder Network

For each folder above I can use the command “net use” to map them to my local disks like this

net use T: \\SRVxxxx\Txxxxxxx		/persistent:yes

Then I have a list of network mapped disks with description about how much free space and total space there are. The free space and total space belong of course to the hard disk where these folders lie.

Network mapped disks

So it’s great but if I open server “srvxxxx” in Windows Explorer and take a look at property of the folder which is mapped to my local disks, I can’t see any information about free space, files or something like that.

Network Folder Properties

My question is why I can’t see the information about files and free space when I am browsing directly on server? Only mapping these folders to local hard disk, then these information will be shown. So I tried to answer myself by making a small code to give me these information. First, I would like to list my local hard disks with their spaces. It’s pretty easy with help of DriveInfo class of .Net Framework.

private static void FreeSpaceLocal()
	DriveInfo[] allDrives = DriveInfo.GetDrives();

	foreach (DriveInfo d in allDrives)
		Console.WriteLine("Drive {0}", d.Name);
		Console.WriteLine("  File type: {0}", d.DriveType);
		if (d.IsReady == true)
			Console.WriteLine("  Volume label: {0}", d.VolumeLabel);
			Console.WriteLine("  File system: {0}", d.DriveFormat);
			Console.WriteLine("  Available space to current user:{0, 15} bytes", d.AvailableFreeSpace);
			Console.WriteLine("  Total size of drive:            {0, 15} bytes ", d.TotalSize);

Then I tried with a remote computer in same LAN but the DriveInfo class can not resolve the UNC Path which looks like this “\\computername\c$”. Therefore to get information of remote computer , I need a reference to System.Management


and then I will use WMI to fetch the information of the remote harddisk. Windows Management Instrumentation (WMI) is the infrastructure for management data and operations on Windows-based operating systems. You can write WMI scripts or applications to automate administrative tasks on remote computers but WMI also supplies management data to other parts of the operating system and products, for example System Center Operations Manager, formerly Microsoft Operations Manager (MOM), or Windows Remote Management. (MSDN)

private static void FreeSpaceNetwork(string srvname)
		ConnectionOptions conn = new ConnectionOptions();
		string strNameSpace = @"\\";
		if (srvname != "")
			strNameSpace += srvname;
			strNameSpace += ".";
		strNameSpace += @"\root\cimv2";
		System.Management.ManagementScope managementScope = new System.Management.ManagementScope(strNameSpace, conn);
		System.Management.ObjectQuery query = new System.Management.ObjectQuery("select * from Win32_LogicalDisk where DriveType=3");
		ManagementObjectSearcher moSearcher = new ManagementObjectSearcher(managementScope, query);
		ManagementObjectCollection moCollection = moSearcher.Get();
		foreach (ManagementObject oReturn in moCollection)
			//foreach (PropertyData prop in oReturn.Properties)
			//    Console.WriteLine(prop.Name + " " + prop.Value);

			Console.WriteLine("Drive {0}", oReturn["Name"].ToString());
			Console.WriteLine("  Volume label: {0}", oReturn["VolumeName"].ToString());
			Console.WriteLine("  File system: {0}", oReturn["FileSystem"].ToString());
			Console.WriteLine("  Available space to current user:{0, 15} bytes", oReturn["FreeSpace"].ToString());
			Console.WriteLine("  Total size of drive:            {0, 15} bytes ", oReturn["Size"].ToString());
	catch (Exception ex)

If you want to know more about WMI, I warmly introduced this link http://msdn.microsoft.com/en-us/library/aa394582%28v=vs.85%29.aspx which explains and discusses about how WMI works, what WMI provides and how to use it in your application with many examples or a book if you don’t like to sit for hours in front of computers

VBScript, WMI, and ADSI Unleashed: Using VBScript, WMI, and ADSI to Automate Windows Administration

Although the code about works perfectly with a normal remote computer but when I tried to fetch information of the server I got an error with “Access denied” although I have administrator right and already set administrator login (Username and Password) for the connection. Until now I still don’t understand why it does not work with the server. If you are interested in the source code for getting free space of remote computer, you can get it here “Free Space Harddisk“.


Leave a comment

Your email address will not be published. Required fields are marked *