我已经研究这个主题好几天了,但我找不到任何关于在 MTP 可移植设备(更具体地说是 Galaxy S4)上管理文件的信息。
我希望能够...
- 将文件从 PC 复制到 MTP 设备
- 将文件从 MTP 设备复制到 PC
- 从 MTP 设备中删除文件
我真的很想复制 MP3 文件,但如果有一种通用的方法可以复制并获得 MTP 支持的文件,那就太棒了。我查看了 Window Portable Device API,但找不到任何包含 C# 示例代码的地方。
任何博客、示例代码和文件都会非常有帮助。谢谢! :)
最佳答案
我使用了一个名为 MediaDevices 的 nugetpackage
这让我可以非常轻松地将我的照片从我的 Android 手机复制到我的电脑。
public class Program
{
static void Main(string[] args)
{
var devices = MediaDevice.GetDevices();
using (var device = devices.First(d => d.FriendlyName == "Galaxy Note8"))
{
device.Connect();
var photoDir = device.GetDirectoryInfo(@"\Phone\DCIM\Camera");
var files = photoDir.EnumerateFiles("*.*", SearchOption.AllDirectories);
foreach (var file in files)
{
MemoryStream memoryStream = new System.IO.MemoryStream();
device.DownloadFile(file.FullName, memoryStream);
memoryStream.Position = 0;
WriteSreamToDisk($@"D:\PHOTOS\{file.Name}", memoryStream);
}
device.Disconnect();
}
}
static void WriteSreamToDisk(string filePath, MemoryStream memoryStream)
{
using (FileStream file = new FileStream(filePath, FileMode.Create, System.IO.FileAccess.Write))
{
byte[] bytes = new byte[memoryStream.Length];
memoryStream.Read(bytes, 0, (int)memoryStream.Length);
file.Write(bytes, 0, bytes.Length);
memoryStream.Close();
}
}
}
关于c# - 如何管理 MTP 可移植设备上的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18512737/