相关名词解释:

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[0], 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 / 1024 / 1024 / 1024, 3), Math.Round((double)storageCapacity / 1024 / 1024 / 1024, 3));}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 = 1;//设备数目初始值必须大于0deviceManager.GetDevices(null, ref deviceCount);//获取设备数目必须置第一个参数为nullif (deviceCount > 0){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 = 0,Camera = 1,MediaPlayer = 2,Phone = 3,Video = 4,PersonalInformationManager = 5,AudioRecorder = 6};/// <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 = 15 };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 = 4 };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 = 3 };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 = 7 };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 = 8 };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(0, parentId, null, out objectIds);List<string> childItems = new List<string>();uint fetched = 0;do{string objectId;objectIds.Next(1, out objectId, ref fetched);if (fetched > 0){childItems.Add(objectId);}} while (fetched > 0);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 = 5;_tagpropertykey storageCapacityKey = new _tagpropertykey();storageCapacityKey.fmtid = new Guid("01a3057a-74d6-4e80-bea7-dc4c212ce50a");storageCapacityKey.pid = 4;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了一份,送给有缘人吧:

PortableDeviceApiLib读取WPD设备信息

c#使用PortableDeviceApiLib读取便携式设备(WPD:Windows Portable Devices)信息相关推荐

  1. C# WPD (windows portable devices) 检测WPD设备 获取设备信息

    最近用c#写过一个WPD的列子,主要是参考 c++的实例, 在 windows sdk 中 ( C:/Program Files/Microsoft SDKs/Windows/v7.0/Samples ...

  2. c#使用WPD读取便携式设备信息一(枚举设备、连接设备及读取设备信息)

    手机或其他电子设备通过USB插入电脑上,并且以MTP(媒体传输协议)方式连接时,可在"计算机"中看到类似计算机盘符的便携式设备文件夹显示,但是这并不是一个计算机盘符,并不能通过常规 ...

  3. c#使用wpd读取便携式设备信息二

    在上节内容(c#使用wpd读取便携式设备信息一)中,我们已经获取到了设备名称,容量等信息,本节进行读写设备的存储内容操作.WPD对设备的操作都是基于对象的ID的,例如文件夹和文件都有各自的object ...

  4. go语言 使用MTP协议 通过WPD(windows portable device)读取便携式设备信息并进行文件传输

    最下方有demo及源码. 背景 当手机通过 USB 连接 PC (选择文件传输,也就是MTP方式) 时,会看到设备管理器中出现便携设备这一栏,如下图: 打开我的电脑可以看到设备和驱动器中出现对应的设备 ...

  5. 基于OMAP-L138的便携式设备状态监测与诊断仪设计

    摘要: 提出基于OMAP-L138的便携式设备状态监测与诊断仪的设计方案.介绍了其软硬件结构设计,重点对数据采集部分的硬件及数据采集模块的软件设计做了详细的介绍.利用了ARM核控制处理优势与DSP核数 ...

  6. 移动设备应用程序开发入门一:创建用于设备的 Windows 窗体应用程序并打包进行部署...

    目标:了解如何使用 Visual C# 创建一个简单的 Hello World 应用程序,该应用程序可在 Pocket PC 上运行. 了解如何为应用程序创建随后可部署到智能设备上的 .cab 文件. ...

  7. wince+labview+ARM的便携式设备

    经过了三个月的时间,终于完成了电能质量仪的开发任务!当初接这个项目的时候,心里有些怕怕,因为一直没有在ARM体系下WINCE系统下完成一个项目,所以很有因度,自我感觉!主要有以下几点: 1.labvi ...

  8. 针对上网本和便携式设备的第一批MeeGo构架

    你正关注着本周即将发布的全新移动设备,你可能会想,其他人都还在观望吧?不对,诺基亚告诉你.第一批 MeeGo 代码刚刚发布,现在我正在为我的上网本下载这些代码.在资源库,有三种构架,每个构架支持不同的 ...

  9. win7禁用手机便携式设备_在便携式Firefox(和其他便携式应用)中禁用启动屏幕...

    win7禁用手机便携式设备 Portable applications are cool because you can run them on any machine from your thumb ...

最新文章

  1. Java字符类型练习
  2. 想学Python有没有必要报班?
  3. python爬虫编程100例_哪种Python程序员最赚钱?爬虫数据告诉你!
  4. SCons命令 之 从入门到精通
  5. 必须知道的八大种排序算法【java实现】
  6. 我的JMX心得 -- Server端
  7. oracle exp consistent,exp CONSISTENT=Y 原理:export前发出SET TRANSACTION READ ONLY命令
  8. css两列等高,css 多列等高
  9. tableView cell 中如果有文本框点击自动滚动不被键盘挡住
  10. Linux内核分析 - 网络[十二]:UDP模块 - socket
  11. python程序文件的扩展名称是什么_python程序文件的扩展名称是什么_Python教程,python,扩展名...
  12. python多因子量化选股模型_量化新兵第十步:多因子选股模型
  13. mysql union all 别名_mysql union 与 union all 语法及用法
  14. 360浏览器:中国为什么没有自主研发的浏览器内核?
  15. 一文告诉你Java素数怎么判断
  16. office visio 替代_5款替代微软Visio的开源免费软件(转)
  17. mysql 左连接 左外连接吗_数据库左连接和左外连接有区别吗
  18. iPhone卡三格信号网络不通无服务
  19. 集合源码解析Map容器Gc回收算法
  20. 第11课:词句分布式表达——词建模工具

热门文章

  1. 【代码复现】ubuntu18.04复现DID-MDN问题总结
  2. yolact模型DCNv2模块编译错误解决方法
  3. 五-中, Spark 算子 吐血总结(转化+行动算子共三十七个)
  4. 解决通用串行总线控制器里全是叹号问题
  5. Linux系统调用 - 文件操作
  6. 我在b站学数据库 (七):多表操作
  7. 美团专家:35岁是程序员的终点?
  8. 华为设备BGP基本配置
  9. 【Visual C++】游戏开发笔记四十二 浅墨DirectX教程之十 游戏输入控制利器:DirectInput专场...
  10. matlab 光栅 傅里叶,光栅原理及MATLAB仿真汇编.doc