gpt4 book ai didi

c# - 如何从 Windows 7 上的 C# 程序读取 Android 手机上的文件?

转载 作者:行者123 更新时间:2023-12-03 09:34:41 25 4
gpt4 key购买 nike

当我使用 USB 数据线将我的 Android 手机连接到我的 Windows 7 时,Windows 会弹出一个窗口并在 Computer\HTC VLE_U\Internal storage 向我显示手机的内部存储空间。从 Windows 资源管理器。但是没有与此手机存储关联的驱动器号!在 Windows 资源管理器中,我可以操作文件系统。

如何从 C# 程序操作相同的文件或文件夹?

正如我测试的那样,

DirectoryInfo di = new DirectoryInfo(@"C:\"); 

有效,但是
DirectoryInfo di = new DirectoryInfo(@"Computer\HTC VLE_U\Internal storage");

失败的。

但在 Windows 资源管理器中,它是 Computer\HTC VLE_U\Internal storage !没有盘符!

是的,这是 MTP 设备。

我看到了 answer在堆栈溢出中,但运行此代码后返回结果对我来说是空的
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;

我猜对了 drives .但安卓手机的内部存储不在这里。
两个 removableFatDrivesandroids对我来说是空的。

最佳答案

我使用了 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();
}
}
}

关于c# - 如何从 Windows 7 上的 C# 程序读取 Android 手机上的文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25026799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com