吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1650|回复: 2
收起左侧

[其他转载] 读取手机MTP存储设备内的照片

[复制链接]
cjzzz 发表于 2022-4-9 18:19
从网上搜索到一个powershell 代码,代码并不多,改写成c#使用com对象 Shell.Application
image.png
com对象 vba比较友好, 先用vba测试, 再改成c#
[C#] 纯文本查看 复制代码
void Main()
{
	var shellobj = new shellObjectClass();
	shellobj.Run();
}


class shellObjectClass
{
	//ShellSpecialFolderConstants 
	//https://docs.microsoft.com/en-us/windows/win32/api/shldisp/ne-shldisp-shellspecialfolderconstants
	const int ssfDRIVES = 0x11;

	//MTP WPD Storage GUID  
	//https://docs.microsoft.com/en-us/uwp/api/Windows.Devices.Portable	
	const string wpdGuid = "6AC27878-A6FA-4155-BA85-F98F491D4F33";

	public string dstPath { get; set; } = @"f:\dcim";

	public void Run()
	{
		try
		{
			//MTP存储设备路径
			var rootPath = GetRootPath();

			foreach (var path in rootPath)
			{
				//MTP存储设备目录
				var folderItems = GetFolderItems(path);

				//多级路径
				while (folderItems.Count == 1)
				{
					folderItems = folderItems.item[0].GetFolder.Items;
				}

				//照片文件夹
				foreach (var item in folderItems)
				{
					if (item.Name == "DCIM") { folderItems = item.GetFolder.Items; break; }
				}

				//子文件夹
				foreach (var item in folderItems)
				{
					if (item.Name == "Camera") { folderItems = item.GetFolder.Items; break; }
				}

				//文件筛选
				foreach (var item in folderItems)
				{
					if (item.Name.Contains(".jpg")) { FileCopy(item, dstPath); }
				}

				//释放对象
				folderItems = null;
			}

		}
		catch (Exception ex)
		{
			Debug.WriteLine(ex);
		}
	}

	private string[] GetRootPath()
	{
		var pathList = new List<string>();
		try
		{
			Type shellType = Type.GetTypeFromProgID("Shell.Application");
			dynamic shellObject = System.Activator.CreateInstance(shellType);
			var objFolder = shellObject.NameSpace(ssfDRIVES).items;
			foreach (var item in objFolder)
			{
				string path = item.Path;
				if (Regex.Match(path, wpdGuid, RegexOptions.IgnoreCase).Success)
				{
					Console.WriteLine($"{item.Name},{item.Type},{item.Path}");
					pathList.Add(path);
				}
			}
			shellObject = null;
		}
		catch (Exception ex)
		{
			Debug.WriteLine(ex);
		}
		return pathList.ToArray();
	}

	private dynamic GetFolderItems(string path)
	{
		try
		{
			Type shellType = Type.GetTypeFromProgID("Shell.Application");
			dynamic shellObject = System.Activator.CreateInstance(shellType);
			return shellObject.NameSpace(path).items;
		}
		catch (Exception ex)
		{
			Debug.WriteLine(ex);
			return new object[0];
		}
	}

	private void FileCopy(dynamic srcItem, string dst)
	{
		//Folder.CopyHere method
		//https://docs.microsoft.com/en-us/windows/win32/shell/folder-copyhere
		try
		{
			Type shellType = Type.GetTypeFromProgID("Shell.Application");
			dynamic shellObject = System.Activator.CreateInstance(shellType);
			var dstObj = shellObject.NameSpace(dst);
			Console.WriteLine(srcItem.Name); 
			dstObj.CopyHere(srcItem, 8);   //参数无效?
		}
		catch (Exception ex)
		{
			Debug.WriteLine(ex);
		}
	}
}

免费评分

参与人数 3吾爱币 +6 热心值 +2 收起 理由
wushaominkk + 5 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
Tonyha7 + 1 用心讨论,共获提升!
disclaimer + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

Tonyha7 发表于 2022-4-9 20:54
牛的 学习了
disclaimer 发表于 2022-4-9 20:43
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-9 22:47

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表