views:

674

answers:

2

How do I get the size (free, total) of any drive on a Windows Mobile phone using C#?

I need to do this with code running on the device (not on a connected PC).

+1  A: 

Assuming the phone's drive is mounted as a regular volume in Windows (i.e., it has a drive letter in Windows Explorer), you can obtain this information using the System.IO.DriveInfo class.

Next problem, of course, would be to determine which drive letter to use, as that will be different on just about every PC the phone is plugged into.

A couple of options for that:

  • Scanning all available drives on the system for a special file that's guaranteed to be present on the phone. Environment.GetLogicalDrives will give you the list of all available drives on a system; some of these may be removable or otherwise inaccessible, though, which can cause some undesirable side-effects during your scan...
  • A more advanced approach would be to enumerate all available USB devices, and find your phone using its manufacturer/device ID. Although written for a slightly different purpose, this CodeProject article should point you in the right direction...

And in case you want to run your code directly on the phone, and not on the PC it's attached to (your question doesn't elaborate on that...), this StackOverflow question shows how to obtain free space information on the Compact Framework.

EDIT, in response to the elaboration that "when i use DriveInfo and give it the drive symbol it gives me the size of the local drive " My C: drive " not the C drive exists on the mobile": the C: drive of the phone will have a different drive letter (for example, F:) on the PC it's mounted on -- therefore the part of my answer that talks about finding the right drive letter. This does, of course, assume that your phone's drive is made available in its entirety on the host PC: depending on the phone model, this may or may not be the case. If not, running some code on your phone is the only viable solution for getting this information...

mdb
Dear mdb;actually i know the drive symbol that i want to get its size but my questions is : when i use DriveInfo and give it the drive symbol it gives me the size of the local drive " My C: drive " not the C drive exists on the mobile.
Eng.Basma
This is a WinMo device, so DriveInfo doesn't exist.
ctacke
Also, if he wanted to run the code on the PC, your answer is incorrect. CE devices don't get mounted as a drive (it's shell-extension trickery that's used to get it into Explorer). The *only* way would be via a custom RAPI call.
ctacke
+4  A: 

I've rewritten the answer based on a better understanding of the question, but without losing any of my original answer for those who find this question.

Getting the size from code running on the device

The answer is simple: P/Invoke GetDiskFreeSpaceEx directly (example here) or use a third-party library that does it for you.

I'm also going to edit the post to make it seem like my assumptions are correct - change them to read otherwise if you need to.

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");

Getting the size from code running on the PC

While the device shows up in Explorer like a "drive", it is not a drive. A shell extension is used to get it to appear in Explorer. You cannot use any drive APIs, in the framework or via P/Invoke, to get information about the device.

If you need to get information about a device connected via ActiveSync (XP and earlier) or WMDC (Vista) then you have to use the Remote API, or RAPI. The specific API is CeGetStoreInformation, but you have to do some initialization before you can call it.

RAPI is a bit too complex to cover in an answer here, but it's well documented online, and there is a ready-made managed wrapper for it (free and open source) here. The specific call you are after in that library is RAPI.GetDeviceStoreInformation.

ctacke
thank you for your help .. but another question where should i add the .dll file should i add it as an item to the project or should i put it on a certain folder
Eng.Basma
if you saw the example you will find that there is a dll file called coredll.dll so when i run the application he didn't see it and that might bcoz i put it in an invalid path
Eng.Basma
You don't "put" coredll.dll anywhere. It's already on the device (it's part of the OS).
ctacke
sry ctacke, i know my questions are alot but i'm new in C# and this is my first application..now i read " P/Invoke GetDiskFreeSpaceEx directly (example here)" i visited the link..and now i don't know what to do should i right the function GetDiskFreeSpaceEx() in my Form class or what to do ?!?thnx
Eng.Basma
Dear eng.ctacke i think there is missunderstanding..in my application i call "ListAllPhone()"which call "ShowFolders" which gets all dirs on phone "E:,F: ..etc" that's why i use CONADefinition.CONA_Folder_Info but i found on its instance that it doesn't support the size of the drive
Eng.Basma
and i found "pTotalSize,pFreeSize" variables on the instance of CONA.Definition.CONA_Folder_Info2 that's why i thought to use it and when i did so it gave me an error.
Eng.Basma
There certainly is. There is no ListAllPhone or ShowFOlders API in Windows or the .NET Framework. You keep talking about CONADefinition, but we have no idea what it is. Windows Mobile has no concept of drive letters, so there are no "E:, F:, etc" drives. Tell us everything about your environment.
ctacke
STOP talking about this magical CONADefinition thing until you say what it is. I have no idea, nor does anyone else who does WinMo development. It is not a WinMo struct, and Google doesn't even help.
ctacke
*Exactly* what make and model of phone is this? *Exactly* what is the OS it's running?
ctacke
you will find this class on "DS_Nokia_PC_Connectivity_API_3_2.pdf" and also you have 2 APIs "Device Managment API and File System API"you will find this class on File System API .
Eng.Basma
and about ListAllPhones(), ShowFolders() these are not APIs and i didn't say so, they are my functions where i manage phone connections
Eng.Basma
Argh! So this isn't even a Windows Mobile phone in the first place!?!? Why couldn't you have eitehr a) SAID what it was in the first place or B) Corrected me (and this question) when I made that assumption? Now you've wasted both your time and mine.
ctacke
I'd recommend you start another question (the answer here matches this question, so is still valuable), this time specifically asking about the Nokia API you're calling, the failure you see and the exact phone model you have. We cannot read minds and Nokia isn't the only phone OEM.
ctacke
sry that was my mistake .. as i said before i'm new in this field
Eng.Basma