c# Problem: Call to GetDiskFreeSpaceExA API not Working!!

D

David

I am attempting to use the GetDiskFreeSpaceExA api to get the
disksapce for a drive. When I call the function from C#, I receive an
OK rc but the space is for my default drive "c:". No matter what path
(UNC) I pass, I always get the default drive space. I tried passing
the path as a constant, a string, etc., same result. Is there a trick
for UNC in C# or is C(You will bang your head until you hear the
ocean!)

This is the first API I have called from C#, and I may be putting the
DllImport in the wrong area.

Any idea?

Also, when I call this same function from vb6, the result is correct.

Status = GetDiskFreeSpaceEx("\\ATC1\SYS\",
BytesAvailableToCaller, _
TotalBytes, FreeBytes)


public class gSpace
{

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


public void space()
{


bool rc;
long FreeBytesAvailable = new long();
long TotalNumberOfBytes = new long();
long TotalNumberOfFreeBytes = new long();
String ss = "\\ATC1\SYS\";
rc = GetFreeDiskSpaceEx(ss,ref FreeBytesAvailable, ref
TotalNumberOfBytes, ref TotalNumberOfFreeBytes);
Console.WriteLine("DISK: " + ss + " " + rc + " " +
FreeBytesAvailable + " " + TotalNumberOfBytes + " " +
TotalNumberOfFreeBytes);
}
 
D

Dan Ferguson

Try this...

(Note: change the "ServerName" and "ShareName" to yours)

using System;
using System.Runtime.InteropServices;

namespace DiskFreeSpaceEx
{
class FreeSpace
{
[DllImport("kernel32")]
public static extern int GetDiskFreeSpace(
string lpRootPathName,
out int lpSectorsPerCluster,
out int lpBytesPerSector,
out int lpNumberOfFreeClusters,
out int lpTotalNumberOfClusters
);

[DllImport("kernel32")]
public static extern int GetDiskFreeSpaceEx(
string lpDirectoryName,
ref long lpFreeBytesAvailable,
ref long lpTotalNumberOfBytes,
ref long lpTotalNumberOfFreeBytes
);

[STAThread]
static void Main(string[] args)
{
string lpRootPathName = @"\\ServerName\ShareName\";
int lpSectorsPerCluster;
int lpBytesPerSector;
int lpNumberOfFreeClusters;
int lpTotalNumberOfClusters;

int bRC = GetDiskFreeSpace(lpRootPathName, out lpSectorsPerCluster, out
lpBytesPerSector, out lpNumberOfFreeClusters, out lpTotalNumberOfClusters);
Console.WriteLine( "{0}\t{1}\t{2}\t{3}\t{4}", lpRootPathName,
lpSectorsPerCluster, lpBytesPerSector, lpNumberOfFreeClusters,
lpTotalNumberOfClusters);

string lpDirectoryName = @"\\ServerName\ShareName\";
long lpFreeBytesAvailable = 0;
long lpTotalNumberOfBytes = 0;
long lpTotalNumberOfFreeBytes = 0;

bRC = GetDiskFreeSpaceEx(lpDirectoryName, ref lpFreeBytesAvailable, ref
lpTotalNumberOfBytes, ref lpTotalNumberOfFreeBytes);
Console.WriteLine( "{0}\t{1}\t{2}\t{3}", lpDirectoryName,
lpFreeBytesAvailable, lpTotalNumberOfBytes, lpTotalNumberOfFreeBytes);
}
}
}

If you still get zeros, check that you have adequate permissions on the
Server\Share, I had to RunAs Administrator to get UNC paths to work.

Dan
 

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