user1591154
user1591154

Reputation: 53

Writing files to an Android in C#

I am writing a Windows Forms application in C#, and I want to copy files from a directory on my C:\ drive into the "Computer\SPH-D710\Phone\Music" directory on my Android phone. That music path I just copied and pasted from Windows Explorer to this post, but C# does not recognize it as a valid directory, probably because there is no "C:\" or the like.

What is a C# command that can write a file to an Android?

Upvotes: 4

Views: 5834

Answers (2)

lahsrah
lahsrah

Reputation: 9173

It appears your phone is connected as a MTP device (media transfer protocol). It is not as simple as a file system access (USB mass storage). Read these series of blogs on how to do it, its not a simple file copy.

https://learn.microsoft.com/en-us/archive/blogs/dimeby8/sending-mtp-commands-through-wpd-part-1-without-a-data-phase

https://learn.microsoft.com/en-us/archive/blogs/dimeby8/sending-mtp-commands-through-wpd-part-2-data-to-the-device

https://learn.microsoft.com/en-us/archive/blogs/dimeby8/sending-mtp-commands-through-wpd-part-3-data-from-device

Beware you will be diving into some unmanaged code for this.

Upvotes: 2

jason.zissman
jason.zissman

Reputation: 2800

The .NET libraries used to store files won't really care where they are saving the file. Your Android device is seen as a USB storage device (as long as USB storage is turned on from the phone's point of view!). I recommend that you put some of your C# code up so that we can potentially spot an error in the routine that you are running to actually save the file to the phone.

As a quick example - your C# code will likely want to look something like this:

string filePath = @"G:\Music\MySong.mp3";
System.IO.File.WriteAllBytes(filePath, mp3FileContent);

Upvotes: 0

Related Questions