繁体   English   中英

如何使用C#获得MTP设备的可用存储和存储容量?

[英]How to get MTP device Available Storage & Storage Capacity using C#?

我正在使用PortableDevice API获取MTP设备检测和设备属性。 我想获得MTP设备存储,例如存储容量和可用存储。这是获取设备友好名称的示例代码,

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);

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

提前致谢。

//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);
}

仅供参考,这是一些代码来显示支持的属性键。 如果您传递的不是第一个文件夹的objectID,而是第一个文件夹(在Explorer中,例如Internal Storage),您将看到

WPD_STORAGE_CAPACITY _tagpropertykey

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

我认为您可能应该浏览一下cgeers教程,因此我将以此为基础。

  1. 将Root文件夹添加到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); 

此代码与cgeers的Refresh方法(和子方法)的一部分非常相似,因此必须已经创建了文件夹对象。

您可以从此文件夹中检索此信息的事实是纯粹的常识/常识(Win 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM