Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Win32 function must I use to copy a file to a smartphone folder?

Using WindowsForms technology, I'm trying to copy a file that is locally stored on the hard drive (C:\) to a folder stored on a connected smartphone device via USB.

The folder "path" is represented using friendly names as MyPCName\MyName\Card\Android in the Explorer navigation bar, and as ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\\?\usb#vid_04e8&pid_6860&ms_comp_mtp&samsung_android#6&612ff8b&1&0000#{6ac27878-a6fa-4155-ba85-f98f491d4f33}\SID-{20002,SECZ9519043CHOHB01,31426543616}\{01A00139-011B-0124-3301-29011C013501} internally in Windows.

I obtained that "internal path" by using the COM Shell.BrowseForFolder method then checking the FolderItem.Path property of the returned object.

Then after getting the path I tried both the CopyFile and CopyFileEx Win32 functions to copy the file but they didn't work. They seemed to be unable to recognize the directory path.

The syntax I used was like this:

Dim dirPath As String = "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\\?\usb#vid_04e8&pid_6860&ms_comp_mtp&samsung_android#6&612ff8b&1&0000#{6ac27878-a6fa-4155-ba85-f98f491d4f33}\SID-{20002,SECZ9519043CHOHB01,31426543616}\{01A00139-011B-0124-3301-29011C013501}"
NativeMethods.CopyFile("C:\MyFile.ext", dirPath & "\MyFile.ext", failIfExists:=True)

In that code the CopyFile() function returns False and the Marshal.GetLastWin32Error() function returns a 0x3 Win32 error code.

The CopyFile/CopyFileEx definitions I used were the same as those published on the Pinvoke.net website (C# versions):

  • http://www.pinvoke.net/default.aspx/kernel32/CopyFile.html
  • http://www.pinvoke.net/default.aspx/kernel32.copyfileex

If a user can copy the file just dragging it from an Explorer instance to the smartphone directory, then I think it is obvious that this can be reproduced programmatically just finding and using the same Win32 functions that Windows uses itself to perform that kind of copy operation from the UI side. Then what am I doing wrong? Why can CopyFile/CopyFileEx not copy the file? And how can I copy it?

Note that I'm looking for a solution written in C# or else VB.NET that can be solved just using managed code or else employing unmanaged code P/Invoking the Win32 functions, except the usage of COM libraries like the Shell COM objects (that provides a CopyHere() method). I would like to learn and to understand how I could do this kind of file copy operation using the Win32 API members.

like image 447
ElektroStudios Avatar asked Mar 05 '17 09:03

ElektroStudios


People also ask

Which method do you use to create a copy of file or folder?

Right-click and pick Copy, or press Ctrl + C . Navigate to another folder, where you want to put the copy of the file. Click the menu button and pick Paste to finish copying the file, or press Ctrl + V . There will now be a copy of the file in the original folder and the other folder.


1 Answers

You are looking for the Windows Portable Devices API.

https://msdn.microsoft.com/en-us/library/windows/desktop/dd388998(v=vs.85).aspx

Edit: I have successfully used this API, using COM Interop in C#, to read/write files on my Android Galaxy S3.

You can also find good information about using this API in C# at the following blog: https://github.com/geersch/WPD

like image 198
FrantzX Avatar answered Sep 30 '22 04:09

FrantzX