Drive Space Polling

J

John Cantley

I need to write an app that keeps check on our drives. I have tried using
DirectoryInfo and Directory but it is way too slow. I have a new 2 terrabyte
server going into production on Monday. Is there a way to just get the size,
space used and space available, just like in the properties when looking at
a drive?

jc
 
N

Nicholas Paldino [.NET/C# MVP]

John,

You should be able to call the GetDiskFreeSpaceEx API function through
the P/Invoke layer. The definition is as follows:

[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern bool GetDiskFreeSpaceEx(
[MarshalAs(UnmanagedType.LPTStr)] string lpDirectoryName,
[MarshalAs(UnmanagedType.U8)] long lpFreeBytesAvailable,
[MarshalAs(UnmanagedType.U8)] long lpTotalNumberOfBytes,
[MarshalAs(UnmanagedType.U8)] long lpTotalNumberOfFreeBytes);

Hope this helps.
 
J

John Cantley

Nicholas;

Here is what I have

public class WinApi
{
[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern bool GetDiskFreeSpaceEx(
[MarshalAs(UnmanagedType.LPTStr)] string lpDirectoryName,
[MarshalAs(UnmanagedType.U8)] long lpFreeBytesAvailable,
[MarshalAs(UnmanagedType.U8)] long lpTotalNumberOfBytes,
[MarshalAs(UnmanagedType.U8)] long lpTotalNumberOfFreeBytes);
}

protected void GetFreeSpace()
{
long lFreeBytes = 0;
long lTotBytes = 0;
long lTotFreeBytes = 0;
string sDir = "C:\\";
bool bOk = WinApi.GetDiskFreeSpaceEx(sDir, lFreeBytes, lTotBytes,
lTotFreeBytes);
if (bOk)
Console.Write(lFreeBytes.ToString());
}

I don't get any values for anything, am I missing another step?

jc
You should be able to call the GetDiskFreeSpaceEx API function through
the P/Invoke layer. The definition is as follows:

[DllImport("kernel32.dll", SetLastError=true, CharSet=CharSet.Auto)]
public static extern bool GetDiskFreeSpaceEx(
[MarshalAs(UnmanagedType.LPTStr)] string lpDirectoryName,
[MarshalAs(UnmanagedType.U8)] long lpFreeBytesAvailable,
[MarshalAs(UnmanagedType.U8)] long lpTotalNumberOfBytes,
[MarshalAs(UnmanagedType.U8)] long lpTotalNumberOfFreeBytes);

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

John Cantley said:
I need to write an app that keeps check on our drives. I have tried using
DirectoryInfo and Directory but it is way too slow. I have a new 2 terrabyte
server going into production on Monday. Is there a way to just get the size,
space used and space available, just like in the properties when looking at
a drive?

jc
 
W

Willy Denoyette [MVP]

Just take a look at the Management classes (WMI) you can find a way to trigger events when a certain threshold has been reached.

Willy.
 

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