3

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

Community
  • 1
  • 1
BDeveloper
  • 1,175
  • 6
  • 24
  • 44

2 Answers2

2

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);
}
casperOne
  • 73,706
  • 19
  • 184
  • 253
  • what is the difference between InternalGetDiskFreeSpaceEx and GetDiskFreeSpaceEx and the function "GetDiskFreeSpaceEx" has something wrong ?!? where identifier is expected where it should be – BDeveloper Jan 03 '09 at 16:53
  • dear casperOne?!? Unfotuanatly it didn't work with me i found this error : Unable to load DLL 'coredll.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E) do your what is that ?!?! – BDeveloper Jan 03 '09 at 16:56
  • Are you running this *on the device*? I don't think you've given us a clear picture of what you're doing. This *must* be a Smart Device application (like I guessed in your original question) and it must be running on the device. – ctacke Jan 03 '09 at 16:59
  • No clue why you're using the "InternalGetDIskFreeSpace" to begin with - it's an alias, and they can cause hard-to-detect bugs. Don't use them unless you know what you're doing (which you apparently don't). You really need to read up on P/Invoking. – ctacke Jan 03 '09 at 17:00
  • See: http://msdn.microsoft.com/en-us/library/aa446536.aspx http://msdn.microsoft.com/en-us/library/aa446538.aspx http://msdn.microsoft.com/en-us/library/aa288468.aspx http://www.codeproject.com/KB/mobile/compframe1.aspx http://www.codeproject.com/KB/mobile/compframe2.aspx And many more. – ctacke Jan 03 '09 at 17:03
0

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");
Community
  • 1
  • 1
ctacke
  • 66,480
  • 18
  • 94
  • 155
  • maybe i dont have a clear knowladye of smart devices briefly my application is a desktop application which allows users to copy some files from server to their phone before that i want to get the size of the memory they use(i read all drive on phone and diplay them with there contents on a treeView) – BDeveloper Jan 03 '09 at 17:09
  • You should have made this clear in the original question instead of making it confusing and then opening another with the same question. The answer is in my original answer to the original question, at the bottom. It's a custom RAPI DLL you have to write in C. – ctacke Jan 03 '09 at 17:14
  • look dear .. my problem spicificly is that he doesn't see the dll file and i dont know what to do ... i read about pInvoke but what next he still didn't see dll file – BDeveloper Jan 03 '09 at 17:15
  • i dont know C language and how is that ?!?! – BDeveloper Jan 03 '09 at 17:19
  • is there another way to get the size i found CONADifinition.CONA_GET_Folder_Info2 but its gives me error too – BDeveloper Jan 03 '09 at 17:23
  • It has nothing to do with the coredll.dll file. Even if you had it you couldn't use it. You're going down the wrong path. I've added more to my answer in the original question (where this discussion should be anyway). Stop adding to this question as it should be closed. Use the original. – ctacke Jan 03 '09 at 17:24
  • What on earth is CONADefiniton anyway? It's certainly not anything standard, nor does it have any bearing on what you're trying to do. – ctacke Jan 03 '09 at 17:25