Question

Exact Duplicate of: Getting the size (free,total) of a Windows Mobile phone drive using c#


dear all;

i know my problem took alot of time and many of u helped me but i'm new in C# and this is my first application..

now i read an article:

C# Signature:

  [DllImport("coredll.dll", SetLastError=true, CharSet=CharSet.Auto)]
  [return: MarshalAs(UnmanagedType.Bool)]
  static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
                                        out ulong lpFreeBytesAvailable,
                                        out ulong lpTotalNumberOfBytes,
                                        out ulong lpTotalNumberOfFreeBytes);

Sample Code:

   ulong FreeBytesAvailable;
   ulong TotalNumberOfBytes;
   ulong TotalNumberOfFreeBytes;

   bool success = GetDiskFreeSpaceEx("C:\\", out FreeBytesAvailable, out  
                                    TotalNumberOfBytes,out TotalNumberOfFreeBytes);
   if (!success)
         throw new System.ComponentModel.Win32Exception();

   Console.WriteLine("Free Bytes Available:      {0,15:D}", FreeBytesAvailable);
   Console.WriteLine("Total Number Of Bytes:     {0,15:D}", TotalNumberOfBytes);
   Console.WriteLine("Total Number Of FreeBytes: {0,15:D}", TotalNumberOfFreeBytes);

now how to use this function GetDiskFreeSpaceEx , and should i add C# signature to somewhere ?!? and what about the coredll.dll ?!?

my code is like that :

  FolderInfo = (CONADefinitions.CONAPI_FOLDER_INFO)Marshal.PtrToStructure(Buffer,     
                                      typeof(CONADefinitions.CONAPI_FOLDER_INFO));
  if (FolderInfo.pstrName[0].ToString() != "C" && level == 0) 
  {
      // here i want to get the Total Size of the currentDirectory and freeSize
      // i want them in Bytes
  }

i searched on google but i dont have enough exprience to know the right tag

thnx

Was it helpful?

Solution

Basically, you call it like any other static method, in your case, like this:

FolderInfo = (CONADefinitions.CONAPI_FOLDER_INFO)Marshal.PtrToStructure(Buffer, typeof(CONADefinitions.CONAPI_FOLDER_INFO));
if (FolderInfo.pstrName[0].ToString() != "C" && level == 0) 
{
  ulong FreeBytesAvailable;
  ulong TotalNumberOfBytes;
  ulong TotalNumberOfFreeBytes;

  bool success = GetDiskFreeSpaceEx("C:\\", out FreeBytesAvailable, out TotalNumberOfBytes,out TotalNumberOfFreeBytes);

  if (!success)
    throw new System.ComponentModel.Win32Exception();
}

I would recommend creating a wrapper that will handle the throwing of the exception for you (unless you want to check against the return value every time and not deal with the exception):

[DllImport("coredll.dll", SetLastError=true, CharSet=CharSet.Auto, EntryPoint="GetDiskFreeSpaceEx")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool InternalGetDiskFreeSpaceEx(string lpDirectoryName, out ulong lpFreeBytesAvailable, out ulong lpTotalNumberOfBytes, out ulong lpTotalNumberOfFreeBytes);

static GetDiskFreeSpaceEx(string directoryName, out ulong freeBytesAvailable, out ulong totalNumberOfBytes, out totalNumberOfFreeBytes);
{
  if (!GetDiskFreeSpaceEx(directoryName, out freeBytesAvailable, out totalNumberOfBytes, out totalNumberOfFreeBytes))
    throw new System.ComponentModel.Win32Exception();
}

The call site would then become:

FolderInfo = (CONADefinitions.CONAPI_FOLDER_INFO)Marshal.PtrToStructure(Buffer, typeof(CONADefinitions.CONAPI_FOLDER_INFO));
if (FolderInfo.pstrName[0].ToString() != "C" && level == 0) 
{
  ulong FreeBytesAvailable;
  ulong TotalNumberOfBytes;
  ulong TotalNumberOfFreeBytes;

  GetDiskFreeSpaceEx("C:\\", out FreeBytesAvailable, out TotalNumberOfBytes,out TotalNumberOfFreeBytes);
}

OTHER TIPS

Your original code is really confusing, you can just use original code linked from the previous question original question here. In fact, you shouldn't have opened another question in the first place, but should have added a comment in the original asking for further clarification.

You have a P/Invoke definition. This is simply a static method call. You put it in a class. Something like this:

public class MyClass
{
  [DllImport("coredll.dll", SetLastError=true, CharSet=CharSet.Auto)]
  [return: MarshalAs(UnmanagedType.Bool)]
  internal static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
    out ulong lpFreeBytesAvailable,
    out ulong lpTotalNumberOfBytes,
    out ulong lpTotalNumberOfFreeBytes);
}

Then you call it (no idea what you're trying to do with all of your marshaling stuff):

ulong GetDiskSize(string volumeName)
{
  ulong avail;
  ulong total;
  ulong totalfree;

  MyClass.GetDiskFreeSpaceEx(volumeName, out avail, out total, out totalFree);

  return total;
  // return others as desired
}

Then to use it it's something like this:

ulong diskSize = GetDiskSize("\\Storage Card");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top