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模式的文件传输

完整源码

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Text;usingSystem.Windows.Forms;usingPortableDeviceApiLib;usingSystem.IO;usingSystem.Runtime.InteropServices;usingSystem.Linq;namespaceWindowsFormsApplication1{publicpartialclass Form1 : Form { PortableDeviceManagerClass devMgr =new PortableDeviceApiLib.PortableDeviceManagerClass(); IPortableDeviceValues pValues = (IPortableDeviceValues)new PortableDeviceTypesLib.PortableDeviceValuesClass(); PortableDeviceClass pPortableDevice =new PortableDeviceClass(); PortableDeviceClass ppDevice =newPortableDeviceClass();string deviceID = string.Empty;public Form1() { InitializeComponent(); this.Load += new EventHandler(Form1_Load); } voidForm1_Load(object sender, EventArgs e) { string[] deviceIds = EnumerateDevices(); PortableDevice portableDevice; IPortableDeviceContent deviceContent; IPortableDeviceValues deviceValues = Connect(deviceIds[0],outportableDevice,outdeviceContent);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 =newStringBuilder();//设备支持WIFI则包含网络这个假设备,也是无法获取到容量的foreach(stringstorageIdinstoragesIds.Where(s=>!s.Contains("网络"))) { ulongfreeSpace;ulong storageCapacity; GetStorageCapacityAnFreeSpace(deviceContent, storageId, outfreeSpace,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>publicstring[] EnumerateDevices() { string[] devicesIds = null; PortableDeviceManagerClass deviceManager =newPortableDeviceManagerClass();uint deviceCount = 1;//设备数目初始值必须大于0deviceManager.GetDevices(null,refdeviceCount);//获取设备数目必须置第一个参数为nullif (deviceCount > 0) { devicesIds =newstring[deviceCount]; deviceManager.GetDevices(devicesIds, ref deviceCount); } return devicesIds; } ///<summary>///连接设备///</summary>///<param></param>///<param></param>///<param></param>///<returns></returns>public IPortableDeviceValues Connect(stringDeviceId,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,outdeviceValues);return deviceValues; } ///<summary>///断开设备///</summary>///<param></param>publicvoid Disconnect(PortableDevice portableDevice) { portableDevice.Close(); } ///<summary>///设备类型///</summary>publicenum DeviceType { Generic =0, Camera =1, MediaPlayer =2, Phone =3, Video =4, PersonalInformationManager =5, AudioRecorder =6};///<summary>///获取设备类型///</summary>///<param></param>///<returns></returns>public DeviceType GetDeviceType(IPortableDeviceValues DeviceValues) { _tagpropertykey deviceTypeKey =new _tagpropertykey() { fmtid = newGuid("26d4979a-e643-4626-9e2b-736dc0c92fdc"), pid = 15};uint propertyValue; DeviceValues.GetUnsignedIntegerValue(refdeviceTypeKey,out propertyValue); DeviceType deviceType =(DeviceType)propertyValue;return deviceType; } ///<summary>///获取设备名///</summary>///<param></param>///<returns></returns>publicstring GetDeviceName(IPortableDeviceValues DeviceValues) { _tagpropertykey property =new _tagpropertykey() { fmtid = newGuid("ef6b490d-5cd8-437a-affc-da8b60ee4a3c"), pid = 4};string name; DeviceValues.GetStringValue(refproperty,outname);return name; } ///<summary>///获取固件版本///</summary>///<param></param>///<returns></returns>publicstring GetFirmwareVersion(IPortableDeviceValues DeviceValues) { _tagpropertykey deviceTypeKey =new _tagpropertykey() { fmtid = newGuid("26d4979a-e643-4626-9e2b-736dc0c92fdc"), pid = 3};string firmwareVersion; DeviceValues.GetStringValue(refdeviceTypeKey,outfirmwareVersion);return firmwareVersion; } ///<summary>///获取制造商///</summary>///<param></param>///<returns></returns>publicstring GetManufacturer(IPortableDeviceValues DeviceValues) { _tagpropertykey property =new _tagpropertykey() { fmtid = newGuid("26d4979a-e643-4626-9e2b-736dc0c92fdc"), pid = 7};string manufacturer; DeviceValues.GetStringValue(refproperty,outmanufacturer);return manufacturer; } ///<summary>///获取型号///</summary>///<param></param>///<returns></returns>publicstring GetModel(IPortableDeviceValues DeviceValues) { _tagpropertykey property =new _tagpropertykey() { fmtid =newGuid("26d4979a-e643-4626-9e2b-736dc0c92fdc"), pid =8};string model; DeviceValues.GetStringValue(refproperty,outmodel);return model; } ///<summary>///获取设备或设备下文件夹的所有对象(文件、文件夹)的ObjectId///</summary>///<param></param>///<param></param>///<returns></returns>publicList<string> GetChildrenObjectIds(IPortableDeviceContent content, string parentId) { IEnumPortableDeviceObjectIDs objectIds; content.EnumObjects(0, parentId, null,out objectIds); List<string> childItems = newList<string>();uint fetched = 0;do{string objectId; objectIds.Next(1,outobjectId,reffetched);if (fetched > 0) { childItems.Add(objectId); } } while (fetched > 0);return childItems; } ///<summary>///获取总容量和可用容量///</summary>///<param></param>///<param></param>///<param></param>///<param></param>publicvoid GetStorageCapacityAnFreeSpace(IPortableDeviceContent deviceContent, stringstorageId,outulongfreeSpace,outulong storageCapacity) { IPortableDeviceProperties deviceProperties; deviceContent.Properties(out deviceProperties); IPortableDeviceKeyCollection keyCollection = (IPortableDeviceKeyCollection)new PortableDeviceTypesLib.PortableDeviceKeyCollectionClass(); _tagpropertykey freeSpaceKey =new _tagpropertykey(); freeSpaceKey.fmtid =newGuid("01a3057a-74d6-4e80-bea7-dc4c212ce50a"); freeSpaceKey.pid =5; _tagpropertykey storageCapacityKey =new _tagpropertykey(); storageCapacityKey.fmtid =newGuid("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(reffreeSpaceKey,out freeSpace); deviceValues.GetUnsignedLargeIntegerValue(refstorageCapacityKey,out storageCapacity); } }}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwdwgd.html