c#使用PortableDeviceApiLib读取便携式设备(WPD:Windows Portable Devices)信息
相关名词解释:
WPD( Windows Portable Devices)
译作Windows 便携设备 (WPD) 是一种驱动程序技术,可支持广泛的可移动设备,比如移动电话、数码相机和便携媒体播放器。WPD 提供了标准化的基础结构,以实现应用程序和连接到正在运行 Windows 的 PC 上的便携设备之间的数据传输。WPD 还可为应用程序提供设备及其内容的统一视图以及标准化机制,从而获得访问数据的权限并对数据进行传输。
MTP(Media Transfer Protocol)模式
MTP模式是微软制订的一套媒体传输协议,由微软公司制定的在设备之间进行多媒体文件交换的通信协议,它实现的是把简单的文件复制变成一种协议性的传输方式。MTP既可以实现在USB协议上,也可以实现在TCP/IP协议上,它属于上层的应用协议,而不关心底层传输协议。目前大部分设备的应用都是基于USB协议。
PortableDeviceApiLib
这是微软提供的一个COM类库可以用于获取WPD设备的信息和进行MTP模式的文件传输
完整源码
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using PortableDeviceApiLib;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Linq;
- namespace WindowsFormsApplication1
- {
- public partial class Form1 : Form
- {
- PortableDeviceManagerClass devMgr = new PortableDeviceApiLib.PortableDeviceManagerClass();
- IPortableDeviceValues pValues = (IPortableDeviceValues)new PortableDeviceTypesLib.PortableDeviceValuesClass();
- PortableDeviceClass pPortableDevice = new PortableDeviceClass();
- PortableDeviceClass ppDevice = new PortableDeviceClass();
- string deviceID = string.Empty;
- public Form1()
- {
- InitializeComponent();
- this.Load += new EventHandler(Form1_Load);
- }
- void Form1_Load(object sender, EventArgs e)
- {
- string[] deviceIds = EnumerateDevices();
- PortableDevice portableDevice;
- IPortableDeviceContent deviceContent;
- IPortableDeviceValues deviceValues = Connect(deviceIds[], out portableDevice, out deviceContent);
- string deviceName = GetDeviceName(deviceValues);
- DeviceType deviceType = GetDeviceType(deviceValues);
- string firmwareVersion = GetFirmwareVersion(deviceValues);
- string manufacturer = GetManufacturer(deviceValues);
- string model = GetModel(deviceValues);
- List<string> storagesIds = GetChildrenObjectIds(deviceContent, "DEVICE");
- StringBuilder storagesSb = new StringBuilder();
- //设备支持WIFI则包含网络这个假设备,也是无法获取到容量的
- foreach (string storageId in storagesIds.Where(s=>!s.Contains("网络")))
- {
- ulong freeSpace;
- ulong storageCapacity;
- GetStorageCapacityAnFreeSpace(deviceContent, storageId, out freeSpace, out storageCapacity);
- storagesSb.AppendFormat("可用容量为{0}GB,总容量为{1}GB",
- Math.Round((double)freeSpace / / / , ), Math.Round((double)storageCapacity / / / , ));
- }
- Disconnect(portableDevice);
- textBox1.AppendText(string.Format("当前设备数目:{0}个\r\n", deviceIds.Length));
- textBox1.AppendText(string.Format("连接的设备名称:{0}\r\n", deviceName));
- textBox1.AppendText(string.Format("连接的设备类型:{0}\r\n", deviceType.ToString()));
- textBox1.AppendText(string.Format("连接的设备固件版本:{0}\r\n", firmwareVersion));
- textBox1.AppendText(string.Format("连接的设备制造厂商:{0}\r\n", manufacturer));
- textBox1.AppendText(string.Format("连接的设备型号:{0}\r\n", model));
- textBox1.AppendText(storagesSb.ToString());
- }
- /// <summary>
- /// 枚举所有便携式设备(MTP模式)
- /// </summary>
- /// <returns>返回设备id数组</returns>
- public string[] EnumerateDevices()
- {
- string[] devicesIds = null;
- PortableDeviceManagerClass deviceManager = new PortableDeviceManagerClass();
- uint deviceCount = ;//设备数目初始值必须大于0
- deviceManager.GetDevices(null, ref deviceCount);//获取设备数目必须置第一个参数为null
- if (deviceCount > )
- {
- devicesIds = new string[deviceCount];
- deviceManager.GetDevices(devicesIds, ref deviceCount);
- }
- return devicesIds;
- }
- /// <summary>
- /// 连接设备
- /// </summary>
- /// <param name="DeviceId"></param>
- /// <param name="portableDevice"></param>
- /// <param name="deviceContent"></param>
- /// <returns></returns>
- public IPortableDeviceValues Connect(string DeviceId, out PortableDevice portableDevice, out IPortableDeviceContent deviceContent)
- {
- IPortableDeviceValues clientInfo = (IPortableDeviceValues)new PortableDeviceTypesLib.PortableDeviceValuesClass();
- portableDevice = new PortableDeviceClass();
- portableDevice.Open(DeviceId, clientInfo);
- portableDevice.Content(out deviceContent);
- IPortableDeviceProperties deviceProperties;
- deviceContent.Properties(out deviceProperties);
- IPortableDeviceValues deviceValues;
- deviceProperties.GetValues("DEVICE", null, out deviceValues);
- return deviceValues;
- }
- /// <summary>
- /// 断开设备
- /// </summary>
- /// <param name="portableDevice"></param>
- public void Disconnect(PortableDevice portableDevice)
- {
- portableDevice.Close();
- }
- /// <summary>
- /// 设备类型
- /// </summary>
- public enum DeviceType
- {
- Generic = ,
- Camera = ,
- MediaPlayer = ,
- Phone = ,
- Video = ,
- PersonalInformationManager = ,
- AudioRecorder =
- };
- /// <summary>
- /// 获取设备类型
- /// </summary>
- /// <param name="DeviceValues"></param>
- /// <returns></returns>
- public DeviceType GetDeviceType(IPortableDeviceValues DeviceValues)
- {
- _tagpropertykey deviceTypeKey = new _tagpropertykey() { fmtid = new Guid("26d4979a-e643-4626-9e2b-736dc0c92fdc"), pid = };
- uint propertyValue;
- DeviceValues.GetUnsignedIntegerValue(ref deviceTypeKey, out propertyValue);
- DeviceType deviceType = (DeviceType)propertyValue;
- return deviceType;
- }
- /// <summary>
- /// 获取设备名
- /// </summary>
- /// <param name="DeviceValues"></param>
- /// <returns></returns>
- public string GetDeviceName(IPortableDeviceValues DeviceValues)
- {
- _tagpropertykey property = new _tagpropertykey() { fmtid = new Guid("ef6b490d-5cd8-437a-affc-da8b60ee4a3c"), pid = };
- string name;
- DeviceValues.GetStringValue(ref property, out name);
- return name;
- }
- /// <summary>
- /// 获取固件版本
- /// </summary>
- /// <param name="DeviceValues"></param>
- /// <returns></returns>
- public string GetFirmwareVersion(IPortableDeviceValues DeviceValues)
- {
- _tagpropertykey deviceTypeKey = new _tagpropertykey() { fmtid = new Guid("26d4979a-e643-4626-9e2b-736dc0c92fdc"), pid = };
- string firmwareVersion;
- DeviceValues.GetStringValue(ref deviceTypeKey, out firmwareVersion);
- return firmwareVersion;
- }
- /// <summary>
- /// 获取制造商
- /// </summary>
- /// <param name="DeviceValues"></param>
- /// <returns></returns>
- public string GetManufacturer(IPortableDeviceValues DeviceValues)
- {
- _tagpropertykey property = new _tagpropertykey() { fmtid = new Guid("26d4979a-e643-4626-9e2b-736dc0c92fdc"), pid = };
- string manufacturer;
- DeviceValues.GetStringValue(ref property, out manufacturer);
- return manufacturer;
- }
- /// <summary>
- /// 获取型号
- /// </summary>
- /// <param name="DeviceValues"></param>
- /// <returns></returns>
- public string GetModel(IPortableDeviceValues DeviceValues)
- {
- _tagpropertykey property = new _tagpropertykey()
- {
- fmtid = new Guid("26d4979a-e643-4626-9e2b-736dc0c92fdc"),
- pid =
- };
- string model;
- DeviceValues.GetStringValue(ref property, out model);
- return model;
- }
- /// <summary>
- /// 获取设备或设备下文件夹的所有对象(文件、文件夹)的ObjectId
- /// </summary>
- /// <param name="deviceId"></param>
- /// <param name="parentId"></param>
- /// <returns></returns>
- public List<string> GetChildrenObjectIds(IPortableDeviceContent content, string parentId)
- {
- IEnumPortableDeviceObjectIDs objectIds;
- content.EnumObjects(, parentId, null, out objectIds);
- List<string> childItems = new List<string>();
- uint fetched = ;
- do
- {
- string objectId;
- objectIds.Next(, out objectId, ref fetched);
- if (fetched > )
- {
- childItems.Add(objectId);
- }
- }
- while (fetched > );
- return childItems;
- }
- /// <summary>
- /// 获取总容量和可用容量
- /// </summary>
- /// <param name="deviceContent"></param>
- /// <param name="storageId"></param>
- /// <param name="freeSpace"></param>
- /// <param name="storageCapacity"></param>
- public void GetStorageCapacityAnFreeSpace(IPortableDeviceContent deviceContent, string storageId, out ulong freeSpace, out ulong storageCapacity)
- {
- IPortableDeviceProperties deviceProperties;
- deviceContent.Properties(out deviceProperties);
- IPortableDeviceKeyCollection keyCollection = (IPortableDeviceKeyCollection)new PortableDeviceTypesLib.PortableDeviceKeyCollectionClass();
- _tagpropertykey freeSpaceKey = new _tagpropertykey();
- freeSpaceKey.fmtid = new Guid("01a3057a-74d6-4e80-bea7-dc4c212ce50a");
- freeSpaceKey.pid = ;
- _tagpropertykey storageCapacityKey = new _tagpropertykey();
- storageCapacityKey.fmtid = new Guid("01a3057a-74d6-4e80-bea7-dc4c212ce50a");
- storageCapacityKey.pid = ;
- keyCollection.Add(ref freeSpaceKey);
- keyCollection.Add(ref storageCapacityKey);
- IPortableDeviceValues deviceValues;
- deviceProperties.GetValues(storageId, keyCollection, out deviceValues);
- deviceValues.GetUnsignedLargeIntegerValue(ref freeSpaceKey, out freeSpace);
- deviceValues.GetUnsignedLargeIntegerValue(ref storageCapacityKey, out storageCapacity);
- }
- }
- }
运行截图
一点感想
最近在重构一个PDA程序就是WinCE的。是的老掉牙的设备(系统),工控行业有些地方还在用。早几年前就转了安卓了,这次重构这么个项目其实还是挺用心的。分层开发,用了ORM,还把PDA程序的功能做成了可配置,有时间会另外写文分享。在收集资料的时候发现网上基本上是没有可以使用的源码,现在DIY了一份,送给有缘人吧:
c#使用PortableDeviceApiLib读取便携式设备(WPD:Windows Portable Devices)信息的更多相关文章
- 虚拟机下Linux读取USB设备的问题虚拟机下Linux无法读取USB设备的解决方案
我们在虚拟机中识别USB设备有三种情况导致Linux系统不能读取到USB设备: 1. .当虚拟机的USB服务没有开启的时候 2. 若虚拟机的USB连接的设置选项没有设置好 3. Widows抢先一步, ...
- 读取Android设备的MAC地址
读取Android设备的MAC地址 AndroidUtil.java package com.csdn.android.util; import com.csdn.android.framewor ...
- Windows 10 版本信息
原文 https://technet.microsoft.com/zh-cn/windows/release-info Windows 10 版本信息 Microsoft 已更新其服务模型. 半年频道 ...
- 浅谈.NET(C#)与Windows用户账户信息的获取
原文:浅谈.NET(C#)与Windows用户账户信息的获取 目录 1. 用户账户名称 - 使用Environment类 2. 用户账户信息 - 使用WindowsIdentity和IdentityR ...
- 。 (有些情况下通过 lsof(8) 或 fuser(1) 可以 找到有关使用该设备的进程的有用信息)
umount时目标忙解决办法 标签(空格分隔): ceph ceph运维 osd 在删除osd后umount时,始终无法umonut,可以通过fuser查看设备被哪个进程占用,之后杀死进程,就可以顺利 ...
- USB系列之二:读取USB设备的描述符
在前面的文章中,我们已经给出了USB协议的链接地址,从这篇文章起,我们会涉及到许多USB 1.1的内容,我们的指导思想是先从熟悉USB 1.1协议入手,先使用现成的HCD和USBD,直接面对客户端驱动 ...
- 使用恶意USB设备解锁 Windows & Mac 锁屏状态
NSA专业物理入侵设备——USB Armory,可解锁任意锁屏状态的下的Windows和Mac操作系统,含最新发布的Windows10.及较早的Mac OSX El Capitan / Maveric ...
- 读取注册表获取Windows系统XP/7/8/10类型(使用wcscmp比较wchar[]内容)
很多方案是采用GetVersion.GetVersionEx这两个API来查询操作系统的版本号来判断当前的操作系统是Windows系列中的哪个,在Win10没有出现前,这种方法是行的通的,但是Win1 ...
- 开源的excel读取库libxls在windows下的编译,且支持中文,全网首发
转载请注明出处:http://www.cnblogs.com/superbi/p/5482516.html 到目前为止,网络和官网上并没有关于libxls在windows下支持中文的教程,也没有现成的 ...
随机推荐
- web前端开发初学者必看的学习路线(附思维导图)
很多同学想学习WEB前端开发,虽然互联网有很多的教程.网站.书籍,可是却又不知从何开始如何选取.看完网友高等游民白乌鸦无私分享的原标题为<写给同事的前端学习路线>这篇文章,相信你会有所收获 ...
- .Net Web开发技术栈
有很多朋友有的因为兴趣,有的因为生计而走向了.Net中,有很多朋友想学,但是又不知道怎么学,学什么,怎么系统的学,为此我以我微薄之力总结归纳写了一篇.Net web开发技术栈,以此帮助那些想学,却不知 ...
- 【UML 建模】活动图介绍
1.活动图,即Activity Diagram,是UML中用于对系统的动态行为建模的一种常用工具,它描述活动的顺序,展现从一种活动到另一种活动的控制流.其本质上是一种流程图,着重表现从一个活动到另一个 ...
- MySQL索引(2)
一.索引基础 1. B-Tree索引 <1> 所有的值都是按顺序存储的,并且每一个叶子页到根的距离相同. <2> 顺序组织存储,很适合查找范围数据,效率会非常高. <3& ...
- PHP异常处理机制
1. 异常: 异常(Exception)用于在指定的错误发生时改变脚本的正常流程. 当异常被触发时,通常会发生: (1)当前代码状态被保存: (2)代码执行被切换到预定义的异常处理器函数: (3)根据 ...
- LeetCode 1. Two Sum (两数之和)
Given an array of integers, return indices of the two numbers such that they add up to a specific ta ...
- CNCC2017梳理
大牛云集的中国计算机大会:大会日程表:http://cncc.ccf.org.cn/cn/news/schedule_empty 早上的论坛可以在爱奇艺下载视频 下午的分论坛是多个同时进行的,我也只去 ...
- 前后端分离跨服务器文件上传-Java SpringMVC版
近来工作上不上特别忙,加上对后台java了解一点,所以就抽时间,写了一个java版本的前后端分离的跨服务器文件上传功能,包括前后端代码. 一.Tomcat服务器部分 1.Tomcat服务器 单独复制一 ...
- js中判断鼠标滚轮方向的方法
前 言 LiuDaP 最近无聊,在做自己的个人站,其中用到了一个关于鼠标滚轮方向判断的方法,今天闲来无聊,就给大家介绍一下吧!!!! 在介绍鼠标事件案例前,让我们先稍微了解一下js中的event ...
- poj 2345 Central heating
Central heating Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 678 Accepted: 310 Des ...