Free disk space and total disk space

  • Thread starter Thread starter UJ
  • Start date Start date
Step 1:

Plant this code within your app or within a DLL...

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

.... you will also need the following at the top of that code file...

using System.Runtime.InteropServices;
using Microsoft.Win32;

Step 2:

Make the function call from within your app...

long FreeBytesAvailable = 0;
long TotalNumberOfBytes = 0;
long TotalNumberOfFreeBytes = 0;

Win32API.GetDiskFreeSpaceEx(Path.GetPathRoot(UseADirectoryNameHere),
ref FreeBytesAvailable, ref TotalNumberOfBytes, ref
TotalNumberOfFreeBytes);

....This function will fill the three variables for you. You can then do
whatever you want with them.

HTH,
JP
 

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

Back
Top