Her*_* Yu 8 c# windows android
当我使用 USB 数据线将我的 Android 手机连接到我的 Windows 7 时,Windows 会弹出一个窗口并Computer\HTC VLE_U\Internal storage从 Windows 资源管理器中向我显示手机的内部存储。但是没有与此手机存储链接的驱动器号!在 Windows 资源管理器中,我可以操作文件系统。
如何从 C# 程序操作相同的文件或文件夹?
当我测试时,
DirectoryInfo di = new DirectoryInfo(@"C:\");
Run Code Online (Sandbox Code Playgroud)
有效,但是
DirectoryInfo di = new DirectoryInfo(@"Computer\HTC VLE_U\Internal storage");
Run Code Online (Sandbox Code Playgroud)
失败的。
但在 Windows 资源管理器中,它是Computer\HTC VLE_U\Internal storage!没有盘符!
是的,这是 MTP 设备。
我在 Stack Overflow 中看到了这个答案,但在运行此代码后,返回结果对我来说是空的
var drives = DriveInfo.GetDrives();
var removableFatDrives = drives.Where(
c=>c.DriveType == DriveType.Removable &&
c.DriveFormat == "FAT" &&
c.IsReady);
var androids = from c in removableFatDrives
from d in c.RootDirectory.EnumerateDirectories()
where d.Name.Contains("android")
select c;
Run Code Online (Sandbox Code Playgroud)
我得到正确的drives。但是安卓手机的内部存储不在这里。这两个removableFatDrives和androids是空的我。
我使用了 nugetpackage“Ralf Beckers v1.8.0 的媒体设备”,这使我可以轻松地将照片从设备复制到计算机,反之亦然。
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3980 次 |
| 最近记录: |