5

I am going to create an c# windows application for transfering image file from an android phone to my Wondows PC - when I connect phone with my PC(using data cable). When I given the path "Computer/Nuxes5/..." in C# for accessing files from mobile, (Got from the windows Explorer address bar). Then getting incorrect path. Following is the code I have given for accessing files.

Directory.GetFiles(@"Computer/Nuxes5/...");

Can any one please suggest me, how to access the mobile files using C#.

Sajith A.K.
  • 716
  • 9
  • 21
  • What does the explorer say if you click behind the path it shows in its address bar? Usually it then shows the "real" path that's behind what's in the address bar. – Thorsten Dittmar Apr 27 '15 at 10:52
  • @ThorstenDittmar it isn't such an easy question, the name Explorer display *is* the one posted by the OP. The `DriveInfo` class also *doesn`t* show any drive for the device. Attaching a drive from `Disk Management` would work but I doubt the OP's users would be happy. The OP should probably retrieve the volume Info using WMI or a package like AlphaFS – Panagiotis Kanavos Apr 27 '15 at 10:58
  • 2
    Downvoters who *understood* this question should post an answer. – Panagiotis Kanavos Apr 27 '15 at 10:59
  • @PanagiotisKanavos I'd have expected the system to use some sort of UNC path for connected devices (I didn't check myself when I commented). It may as well be that the device isn't connected as a drive at all, but the Media Transfer Protocol somehow "injects" a new item into the Explorer. – Thorsten Dittmar Apr 27 '15 at 11:05
  • Trying AlphaFS right now, and it *does* show my tabled as an MTP compatible device. No storage device shown in Device Manager, nothing attached under the tablet either. – Panagiotis Kanavos Apr 27 '15 at 11:08
  • While connecting phone it is showing in the "Protable Devices" area in the My Computer. @ThorstenDittmar : I tried that also , same path only showing. – Sajith A.K. Apr 27 '15 at 11:27
  • 1
    All answers I've found until now point to [this](http://cgeers.com/2011/08/13/wpd-transferring-content/) blog post that shows how to use the WPD Automation API. It's not a trivial process. – Panagiotis Kanavos Apr 27 '15 at 11:30
  • @PanagiotisKanavos sadly your link is no longer valid – David Sykes Aug 06 '15 at 14:30

1 Answers1

1

You need to use MTP file transfer. Since you are using Windows, the best thing to do is to use COM with the Windows PortableDeviceApiLib library. This is not an easy task. The WPD API link in one of the comments above is a good reference.

You should also install Microsoft MTP Simulator 3.0 and look at the sample code that comes with it.

In MTP, every file or folder stored on the device is an object with a handle. To retrieve a file or a folder, you have to retrieve the object handle, then check to see if it is a file or a folder by checking its objectFormatCode property. Folders have the object format code set to 0x3001. You can get the entire list from the MTP Spec.

Once you have the WPD/PTP wrapper set up, you can start sending MTP commands to the device. For getting files from the device, the procedure is the following.

  1. Get the available storage ids by calling getStorageIds();
  2. For the storage id you are interested in (internal storage/SD card), call getObjectHandles() to get a list of all files/folders.
  3. Loop through the root folder handle to look for the file you are interested in. For each handle you get, call getObjectInfo() to get the details about that handle.
  4. Once you have a handle whose name and format code matches what you are looking for, call the getObject() function to download the file.

Also remember that you cannot download all the contents at the same time. You have to call getObject() for each file handle you need to download.

avian
  • 41
  • 6