2016-12-14 41 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