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下支持中文的教程,也没有现成的 ...
随机推荐
- CSS之 relative 特性
1. 自身特性: 如left,right,top,bottom定位都是相对于自身位置定位. 当left与right同时存在,lfet生效. 当top与bottom同时存在,top生效. 无侵入,保留原 ...
- 安装xp遇到的问题与如何连接共享的打印机
2013-12-5 星期四 今天下午去给曹老师鼓捣电脑去了,安装了一个xp系统,加上一些常用的办公软件,在连接上一个共享的打印机. 下面是今天我遇到的问题: 问题:安装xp系统之后,没有本地连接,只有 ...
- UWP appButtonBar样式
UWP 的appButtonBar使用<AppBarButton Icon = "Next" Label = "Next" /> Icon是 Sym ...
- VisualStudio 扩展开发
本文主要:如何开发一个 visual Studio 扩展,其实扩展也叫插件. 那么就是如何开发一个 vs插件. 我写这博客时候,是我在开发一个插件:编码规范工具.记录的是我从不知道到发布插件,如果遇到 ...
- Windows-universal-samples-master示例 XamlCommanding
Windows-universal-samples-master XamlCommanding 运行默认如果是 ARM会出现没有引用System,只要在调试选择CPU为PC的就好 默认 选择PC平台 ...
- java中 n-- 和 --n的区别
n--和--n的最终结果都是将n本身减一,区别是现在的值: 比如说: n=10 a=n;此时a=10 b=n--;此时b=10,n=9减号放后面是指先赋值,再自减1 c=--n;此时c=8,n=8减号 ...
- Python 使用正则表达式
当使用正则表达式时,re 模块内部会干两件事情 1.编译正则表达式,如其字串本身不合法,报错 2.用编译后的正则表达式去匹配字符串 re_telephone = re.compile(r'^(\d{3 ...
- session失效问题
具体设置很简单,方法有三种: ()在主页面或者公共页面中加入:session.setMaxInactiveInterval();参数600单位是秒,即在没有10分钟活动后,session将失效. 这里 ...
- LeetCode 119. Pascal's Triangle II (杨辉三角之二)
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...
- Java基础笔记12
1.自定义异常. 定义一个类,让该类继承Exception.并写出该类的所有的构造函数.2.IO流. java.io 文件类.File 字节输入和输出流 InputStream OutputStrea ...