473,385 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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);
}
Nov 15 '05 #1
1 5017
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
Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: xcomm | last post by:
I tried to create some images with php and gd and get always Call to undefined function imagejpeg() or Call to undefined function imagepng() PHP was build with --with-gd --with-zlib but I...
2
by: Hal Halloway | last post by:
I'm trying to go from PHP4 to PHP5, all seemed OK until I tried to connect to a MYSQl DB. Then I get the error message: Fatal error: Call to undefined function mysql_pconnect() in C:\Program...
1
by: Gyoung-Yoon Noh | last post by:
I've written Unix's find-like function using recursive os.listdir() call and generator expression. It seems that error happens in generator expression. Below codes do not work. walk.next()...
1
by: seemanta dutta | last post by:
Hi, I have a simple form which when clicked mails a user via the cgi script. But the problem is that when I run the script from the commandline it works well. but the moment I try to do it via...
2
by: Chris Michael | last post by:
Hello everybody, Newbie here. I've been working on this for the last two days and I can't figure out where this problem is. I think it's something so obvious, but I can't see it! OK, firstly...
3
by: Bob | last post by:
Below is some code that use to work. Now it is not and I have yet to figure out why. I want to select the nodes to a nodelist and remove them from the dom. The only thing I can figure is it broke...
2
by: Zeba | last post by:
Hi guys! I'm new to JS / Ajax; I've been trying to do an Ajax call to my Webservice ( I'm using C# for code-behind). I'm not using any of the libraries available. I am sending my CustID to the...
7
by: lumo2000 | last post by:
hello i am not able to get mysql running. you can get my server/php settings/details here: http://damn-i-have.no-ip.com/phpinfo.php i already enabled the following stuff in my php.ini NOTE:...
6
by: tvnaidu | last post by:
I am trying to create a directory "/log" in linux in below file, it is not creating, I commented windows call and added linux call ro create /log dir. #define DEFAULT_LOG_DIR "/log" void...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.