.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Determine space available on a network drive
 

Determine space available on a network drive

I'm trying to determine if there's enough space for the file on the specified drive before I go ahead and start writing the file out.

If the user specifies a local (or mapped) drive (i.e., "C:\Windows"), I can extract out the drive letter, create an instance of System.IO.DriveInfo and call its AvailableFreeSpace method.

But, System.IO.DriveInfo apparently doesn't support URIs (i.e., "\\server1\users\adam").  I dug around the documentation, but I didn't see any other classes which would provide such information. 

Any suggestions anyone?

TIA,

RichardR
RichardR
Don't think there is a managed class to do this but you can easily use the API.

internal static class Win32
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool GetDiskFreeSpaceEx(string drive, out long freeBytesForUser, out long totalBytes, out long freeBytes);

}

Inside your form access the API as follows: (UNC path is in textBox1):

private void button1_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(textBox1.Text))
{
string pathName = textBox1.Text;
long freeBytesForUser, totalBytes, freeBytes;

if (Win32.GetDiskFreeSpaceEx(pathName, out freeBytesForUser, out totalBytes, out freeBytes))
{
label1.Text = String.Format("Free Space (user): {0}\nFree Space (total): {1}\nTotal Space: {2}",
freeBytesForUser,
freeBytes,
totalBytes);
}
else
{
string errorMessage = String.Format("Unable to obtain free space. Error #{0}.", Marshal.GetLastWin32Error());
MessageBox.Show(errorMessage);
}
}
}

Hope that's what you're looking for.
DotNetHeaven
I am looking for the same thing - the ability to get driver information of other computers on my network.

Any help is greatly appreciated. I am running Visual Studio 2k5.

-Bill English
englishw@gmail.com
englishw
Don't think there is a managed class to do this but you can easily use the API.

internal static class Win32
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool GetDiskFreeSpaceEx(string drive, out long freeBytesForUser, out long totalBytes, out long freeBytes);

}

Inside your form access the API as follows: (UNC path is in textBox1):

private void button1_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(textBox1.Text))
{
string pathName = textBox1.Text;
long freeBytesForUser, totalBytes, freeBytes;

if (Win32.GetDiskFreeSpaceEx(pathName, out freeBytesForUser, out totalBytes, out freeBytes))
{
label1.Text = String.Format("Free Space (user): {0}\nFree Space (total): {1}\nTotal Space: {2}",
freeBytesForUser,
freeBytes,
totalBytes);
}
else
{
string errorMessage = String.Format("Unable to obtain free space. Error #{0}.", Marshal.GetLastWin32Error());
MessageBox.Show(errorMessage);
}
}
}

Hope that's what you're looking for.
DotNetHeaven

Hi,

Thank you very much for the post. I was searching for this answer for few days.

It helped me to get the Free disk space from UNC Path.

Once again Thank you (Dotnet Heaven).

With Regards,
Sathish


sathish_indian

You can use google to search for other answers

Custom Search

More Threads

• How can i made a UserControl with Panel
• SHould I Uninstall .net 1.1 and the 1.1 hotfix before instlaling .net 2?
• SerialPort component problem while ReadLine()
• Robust File System Monitoring
• Loadfrom and CreateInstance failed in Beta2
• Can I use OCR MODI Library (MDIVWCTL.DLL) in .NET in a production server application?
• Programmatically changing permissions on detached files.
• Extension Class
• Serial communication problem reading single charactor. PLEASE HELP!!!!
• How to tell which image formats are available?