Getting the size (free, total) of a Windows Mobile phone drive using C#

前端 未结 2 637
没有蜡笔的小新
没有蜡笔的小新 2021-01-17 02:37

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).

相关标签:
2条回答
  • 2021-01-17 02:52

    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...

    0 讨论(0)
  • 2021-01-17 02:59

    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.

    0 讨论(0)
提交回复
热议问题