相关名词解释:

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了一份,送给有缘人吧:

PortableDeviceApiLib读取WPD设备信息

c#使用PortableDeviceApiLib读取便携式设备(WPD:Windows Portable Devices)信息的更多相关文章

  1. 虚拟机下Linux读取USB设备的问题虚拟机下Linux无法读取USB设备的解决方案

    我们在虚拟机中识别USB设备有三种情况导致Linux系统不能读取到USB设备: 1. .当虚拟机的USB服务没有开启的时候 2. 若虚拟机的USB连接的设置选项没有设置好 3. Widows抢先一步, ...

  2. 读取Android设备的MAC地址

    读取Android设备的MAC地址   AndroidUtil.java package com.csdn.android.util; import com.csdn.android.framewor ...

  3. Windows 10 版本信息

    原文 https://technet.microsoft.com/zh-cn/windows/release-info Windows 10 版本信息 Microsoft 已更新其服务模型. 半年频道 ...

  4. 浅谈.NET(C#)与Windows用户账户信息的获取

    原文:浅谈.NET(C#)与Windows用户账户信息的获取 目录 1. 用户账户名称 - 使用Environment类 2. 用户账户信息 - 使用WindowsIdentity和IdentityR ...

  5. 。 (有些情况下通过 lsof(8) 或 fuser(1) 可以 找到有关使用该设备的进程的有用信息)

    umount时目标忙解决办法 标签(空格分隔): ceph ceph运维 osd 在删除osd后umount时,始终无法umonut,可以通过fuser查看设备被哪个进程占用,之后杀死进程,就可以顺利 ...

  6. USB系列之二:读取USB设备的描述符

    在前面的文章中,我们已经给出了USB协议的链接地址,从这篇文章起,我们会涉及到许多USB 1.1的内容,我们的指导思想是先从熟悉USB 1.1协议入手,先使用现成的HCD和USBD,直接面对客户端驱动 ...

  7. 使用恶意USB设备解锁 Windows & Mac 锁屏状态

    NSA专业物理入侵设备——USB Armory,可解锁任意锁屏状态的下的Windows和Mac操作系统,含最新发布的Windows10.及较早的Mac OSX El Capitan / Maveric ...

  8. 读取注册表获取Windows系统XP/7/8/10类型(使用wcscmp比较wchar[]内容)

    很多方案是采用GetVersion.GetVersionEx这两个API来查询操作系统的版本号来判断当前的操作系统是Windows系列中的哪个,在Win10没有出现前,这种方法是行的通的,但是Win1 ...

  9. 开源的excel读取库libxls在windows下的编译,且支持中文,全网首发

    转载请注明出处:http://www.cnblogs.com/superbi/p/5482516.html 到目前为止,网络和官网上并没有关于libxls在windows下支持中文的教程,也没有现成的 ...

随机推荐

  1. win10 + python3.6 + VSCode + tensorflow-gpu + keras + cuda8 + cuDN6N环境配置

    写在前面的话: 再弄这个之前,我对python也好,tensorflow也好几乎是0认知的,所以配置这个环境的时候,走了不少弯路,整整耗费了一个星期的时间才搭配完整这个环境,简直了...然而最气的是, ...

  2. linux命令行下svn常用命令

    linux命令行下svn常用命令 1. 将文件checkout到本地目录 1 #path是服务器上的目录 2 svn checkout path 3 4 #示例 5 svn checkout svn: ...

  3. css雪碧图(css splite)

    将很多小的背景图片放在一起,可以减少http请求. 这些图片通常是一类的. 所以使用雪碧图. 雪碧图即为: 测试一下减少了多长时间 0 = 0

  4. LeetCode 31. Next Permutation (下一个排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  5. servlet的运行工作

    我是一个刚学几天的小白,写得不好,如果哪个地方有错误,欢迎你们指出. 在服务器端,我用的是Tomcat作为Servlet 容器,在容器中有一个Servlet接口,你编写一个servlet类放在Tomc ...

  6. input file选择图片后 预览

    很多前端都选择用插件来实现图片预览,这个小功能也可以很简单的用jQuery来实现 简单的jQuery实现input file选择图片后,可以预览图片的效果 简单的HTML代码: <div> ...

  7. SAXReader简单实例解析HTML

    转载自:http://blog.csdn.net/seayqrain/article/details/5024068# 使用SAXReader需要导入dom4j-full.jar包. dom4j是一个 ...

  8. java Callable创建线程

    package com.java.concurrent; import java.util.concurrent.Callable; import java.util.concurrent.Execu ...

  9. 【机器学习实战】第13章 利用 PCA 来简化数据

    第13章 利用 PCA 来简化数据 降维技术 场景 我们正通过电视观看体育比赛,在电视的显示器上有一个球. 显示器大概包含了100万像素点,而球则可能是由较少的像素点组成,例如说一千个像素点. 人们实 ...

  10. 服务器 : Apache Tomcat - 理解架构层次

    文章概览 相信很多接触java的人都对Tom猫有着多少的熟悉,就个人而言,本来只知道Tom简单的操作与配置,就像裹上一层纱,迷迷糊糊的. Tomcat的书籍本来就不多,高分的还是很久之前的版本,直到最 ...