Free Space on a Drive Passing UNC Path

G

Guest

I need to pass a UNC path to the code and return the Free space on that drive, Can any one please pass through some samples that will assist.

Thanks ( C# Newbee :) )
 
S

Shakir Hussain

Try this

string myUNCPath = @"\\sys12345"

ConnectionOptions opt = new ConnectionOptions();
ObjectQuery oQuery = new ObjectQuery("SELECT FreeSpace FROM
Win32_LogicalDisk WHERE DriveType = 3");
ManagementScope scope = new ManagementScopemyUNCPath , opt);
ManagementObjectSearcher moSearcher = new ManagementObjectSearcher(scope,
oQuery);
ManagementObjectCollection collection = moSearcher.Get();
foreach(ManagementObject res in collection)
{
decimal freeSpace =
Convert.ToDecimal(res["FreeSpace"])/1024/1024/1024;
MessageBox.Show ("Free Space (GB)- "+ freeSpace.ToString());
break;
}


--
Shak
(Houston)




Glenn Wilson said:
I need to pass a UNC path to the code and return the Free space on that
drive, Can any one please pass through some samples that will assist.
 
W

Willy Denoyette [MVP]

Using System.Management and WMI, try this...

using System;
using System.Management;
class Tester {
public static void Main() {
GetFreeSpace(@"\\\\servername\\sharename"); // UNC path, Note the double
quotes!!!
}
public static void GetFreeSpace(string ProviderName )
{
String strSQL = "SELECT FreeSpace, QuotasDisabled ,VolumeName FROM
Win32_LogicalDisk WHERE providername='" + ProviderName + "'" ;
SelectQuery query = new SelectQuery(strSQL);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject mo in searcher.Get()) {
// Free disk space is irrelevant if per user quota's are enabled!
if(mo["QuotasDisabled"].ToString() != "true")
Console.WriteLine("{0} - Free bytes: {1} ",mo["VolumeName"],
mo["Freespace"]);
else
Console.WriteLine("{0} - Free bytes: {1} per-user quota's
applied!!",mo["VolumeName"], mo["Freespace"]);

}
}
}

Willy.
 
W

Willy Denoyette [MVP]

This won't work, see my other reply in this same thread.
Willy.

Shakir Hussain said:
Try this

string myUNCPath = @"\\sys12345"

ConnectionOptions opt = new ConnectionOptions();
ObjectQuery oQuery = new ObjectQuery("SELECT FreeSpace FROM
Win32_LogicalDisk WHERE DriveType = 3");
ManagementScope scope = new ManagementScopemyUNCPath , opt);
ManagementObjectSearcher moSearcher = new
ManagementObjectSearcher(scope,
oQuery);
ManagementObjectCollection collection = moSearcher.Get();
foreach(ManagementObject res in collection)
{
decimal freeSpace =
Convert.ToDecimal(res["FreeSpace"])/1024/1024/1024;
MessageBox.Show ("Free Space (GB)- "+ freeSpace.ToString());
break;
}


--
Shak
(Houston)




Glenn Wilson said:
I need to pass a UNC path to the code and return the Free space on that
drive, Can any one please pass through some samples that will assist.
Thanks ( C# Newbee :) )
 
G

Glenn Wilson

Thanks Will give it a try at work on Monday,

MatthewWatson on GotDotNet, replied with this api solution, thought you all
might like it as well.

/// <summary>
/// Gets the disk free space on a particular volume.
/// </summary>
/// <param name="lpszPath">Full path of folder on required volume. Must end
with a backslash!</param>
/// <param name="lpFreeBytesAvailable">Free bytes available on the volume
for the current user.</param>
/// <param name="lpTotalNumberOfBytes">Total number of bytes available on
the volume for the current user.</param>
/// <param name="lpTotalNumberOfFreeBytes">Total number of bytes available
on the volume for all users.</param>
/// <returns>True if successful, false on error.</returns>

[DllImport ("Kernel32")]
public static extern bool GetDiskFreeSpaceEx
(
string lpszPath, // Must name a folder, must end with '\'.
ref long lpFreeBytesAvailable,
ref long lpTotalNumberOfBytes,
ref long lpTotalNumberOfFreeBytes
);


/// <summary>
/// Returns free space for drive containing the specified folder, or
returns -1 on failure.
/// </summary>
/// <param name="folder_name">Must name a folder, and MUST end with a
backslash.</param>
/// <returns>Space free on the volume containing 'folder_name' or -1 on
error.</returns>

public long free_space( string folder_name )
{
long free=0, dummy1=0, dummy2=0 ;

if ( Win32API.File.GetDiskFreeSpaceEx( folder_name, ref free, ref dummy1,
ref dummy2 ) )
return free ;
else
return -1 ;
}


Willy Denoyette said:
Using System.Management and WMI, try this...

using System;
using System.Management;
class Tester {
public static void Main() {
GetFreeSpace(@"\\\\servername\\sharename"); // UNC path, Note the double
quotes!!!
}
public static void GetFreeSpace(string ProviderName )
{
String strSQL = "SELECT FreeSpace, QuotasDisabled ,VolumeName FROM
Win32_LogicalDisk WHERE providername='" + ProviderName + "'" ;
SelectQuery query = new SelectQuery(strSQL);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
foreach (ManagementObject mo in searcher.Get()) {
// Free disk space is irrelevant if per user quota's are enabled!
if(mo["QuotasDisabled"].ToString() != "true")
Console.WriteLine("{0} - Free bytes: {1} ",mo["VolumeName"],
mo["Freespace"]);
else
Console.WriteLine("{0} - Free bytes: {1} per-user quota's
applied!!",mo["VolumeName"], mo["Freespace"]);

}
}
}

Willy.

Glenn Wilson said:
I need to pass a UNC path to the code and return the Free space on that
drive, Can any one please pass through some samples that will assist.

Thanks ( C# Newbee :) )
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top