2016-12-14 83 views
0

我正在使用PortableDevice API来获取MTP设备检测和设备属性。 我想得到这样的存储容量的MTP设备存储和可用storage.Here是获取设备的友好名称是我的示例代码,如何获得MTP设备使用C#的可用存储和存储容量?

public string FriendlyName 
    { 
     get 
     { 
      if (!this._isConnected) 
      { 
       throw new InvalidOperationException("Not connected to device."); 
      } 

      // Retrieve the properties of the device 
      IPortableDeviceContent content; 
      IPortableDeviceProperties properties; 
      this._device.Content(out content); 
      content.Properties(out properties); 

      // Retrieve the values for the properties 
      IPortableDeviceValues propertyValues; 
      properties.GetValues("DEVICE", null, out propertyValues); 

      // Identify the property to retrieve 
      var property = new _tagpropertykey(); 
      property.fmtid = new Guid(0x26D4979A, 0xE643, 0x4626, 0x9E, 0x2B, 
             0x73, 0x6D, 0xC0, 0xC9, 0x2F, 0xDC); 
      property.pid = 12; 

      // Retrieve the friendly name 
      string propertyValue; 
      propertyValues.GetStringValue(ref property, out propertyValue); 

      return propertyValue; 
     } 
    } 

同样我也想读设备存储和免费来自MTP设备的空间。

我想这样的,但我错过了一些东西,

IPortableDeviceKeyCollection keys; 
     properties.GetSupportedProperties(objectId, out keys); 

     IPortableDeviceValues values; 
     properties.GetValues(objectId, keys, out values); 

     // Get the name of the object 
     string name; 
     var property = new _tagpropertykey();    
     property.fmtid = new Guid(0x01A3057A, 0x74D6, 0x4E80, 0xBE, 0xA7, 0xDC, 0x4C, 0x21, 0x2C, 0xE5, 0x0A); 
     property.pid = 7; 
     values.GetStringValue(property, out name); 

     // Get the type of the object 
     Guid contentType; 
     property = new _tagpropertykey(); 

     property.fmtid = new Guid(0x01A3057A, 0x74D6, 0x4E80, 0xBE, 0xA7, 0xDC, 0x4C, 0x21, 0x2C, 0xE5, 0x0A); 

     property.pid = 5; 
     values.GetGuidValue(property, out contentType); 

     var storageType = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C); 

     var functionalType = new Guid(0x8F052D93, 0xABCA, 0x4FC5, 0xA5, 0xAC, 0xB0, 0x1D, 0xF4, 0xDB, 0xE5, 0x98); 

........................... ........... ...................................

提前致谢。

回答

0
//Collecting the supported keys 
IPortableDeviceKeyCollection keys; 
properties.GetSupportedProperties(objectId, out keys); 

//Init 
_tagpropertykey key = new _tagpropertykey(); 
uint count = 0; 
keys.GetCount(ref count); 

//temporarily store each key and display 
for (uint i = 0; i < count; i++) 
{ 
    keys.GetAt(i, ref key); 
    Console.WriteLine("fmtid " + key.fmtid + " pid " + key.pid); 
} 

只是FYI这是一些代码来显示支持的属性键。如果传递的objectID没有根文件夹的,但第一个文件夹(在资源管理器中,例如内部存储),你会看到

WPD_STORAGE_CAPACITY _tagpropertykey 

我强烈建议做一个类来存储所有PropertyKeys¹,它会做得更好看。

我想你也许应该在cgeers教程一目了然,所以我会采取的位置。

  1. 添加根文件夹到您PortableDevice类方便地访问:

    private readonly PortableDeviceFolder root = new PortableDeviceFolder("DEVICE", "DEVICE"); 
    public PortableDeviceFolder Root 
    { 
        get 
        { 
         return root; 
        } 
    } 
    
  2. 使用该代码为您的文件夹OBJECTID(如前例如内部存储提到)

    IPortableDeviceProperties properties; 
    content.Properties(out properties); 
    
    IPortableDeviceValues values; 
    properties.GetValues(objectId, keys, out values); 
    
    //capacity stored as UI8 in PropVariant as stated in ² -> ulong 
    ulong capacity = 0; 
    values.GetUnsignedLargeIntegerValue(WPD_STORAGE_CAPACITY_IN_OBJECTS, out capacity); 
    

此代码与来自c的Refresh方法(和子方法)的部分非常相似geers,所以你的文件夹对象已经被创建。

,你可以检索该文件夹此信息的事实,要么是纯粹的知识/常识(赢Explorer还显示该文件夹中的信息),也可以在顶部执行的第一行代码来学习。

我 - 对于我自己 - 更改了PortableDeviceFolder结构,该结构现在包含文件夹中的PortableDeviceObject的集合,并且每个集合还保存其父项。
就像是在文件夹的访问是很容易的,例如,以获得您所需的folderId我只是用这个代码:

PortableDeviceCollection c = new PortableDeviceCollection(); 
c.Refresh(); 
PortableDevice device = c.First(); 
device.Root.RefreshFiles(); 
PortableDeviceFolder internalstorageFolder = (PortableDeviceFolder)device.Root.Files.First(); 

你可以尝试实现这样自己一个结构或去一个完全另一种方式,我认为没有完美的访问结构,所以需要找出最适合的方式。

¹:https://github.com/gtaglang/WpdLib/blob/master/src/WpdLib/WpdProperties.cs

²:https://msdn.microsoft.com/de-de/library/ff597918(v=vs.85).aspx