android – Windows Portable Devices (WPD), C#: How to set "date modified" when creating a file on Portable Device-ThrowExceptions

Exception or error:

If i copy a file from Windows10 with WindowsExplorer to my moto G6 phone, the new created file have imediately the correct ModifyDate like the original file on Windows. Java File.setLastModified() from an app on moto device works also.

I tested to set IPortableDeviceValues with PROPERTYKEY Values from WPD_OBJECT_DATE_MODIFIED in CreateObjectWithPropertiesAndData() in my TransferContentToDevice() code, but its dosn’t work (see code example).

Is it posible to set date modified while copy a file to mobile devices? Or is WindowsExplorer using a different mechanism? If yes, which in detail and can i use it from c#/vb.net? Or is there an other way in PortableDeviceApiLib to change modified date after the file ist created? Any ideas? Thanks in advance!

// typical code according to Christophe Geers
public void TransferContentToDevice(string fileName, string parentObjectId)
    {
        IPortableDeviceContent content;
        this._device.Content(out content);

        IPortableDeviceValues values = GetRequiredPropertiesForContentType(fileName, parentObjectId);

        PortableDeviceApiLib.IStream tempStream;
        uint optimalTransferSizeBytes = 0;
        content.CreateObjectWithPropertiesAndData(values, out tempStream, ref optimalTransferSizeBytes, null);
        ...
        ...
    }

// please see comments
private IPortableDeviceValues GetRequiredPropertiesForContentType(
        string fileName,
        string parentObjectId)
    {
        IPortableDeviceValues values = 
            new PortableDeviceTypesLib.PortableDeviceValues() as IPortableDeviceValues;

        var WPD_OBJECT_PARENT_ID = new _tagpropertykey();
        WPD_OBJECT_PARENT_ID.fmtid = DeviceGUID.ObjectParentID;
        WPD_OBJECT_PARENT_ID.pid = 3 ;
        values.SetStringValue(ref WPD_OBJECT_PARENT_ID, parentObjectId);

        FileInfo fileInfo = new FileInfo(fileName);
        var WPD_OBJECT_SIZE = new _tagpropertykey();
        WPD_OBJECT_SIZE.fmtid = DeviceGUID.ObjectSize;
        WPD_OBJECT_SIZE.pid = 11;            
        values.SetUnsignedLargeIntegerValue(WPD_OBJECT_SIZE, (ulong) fileInfo.Length);

        var WPD_OBJECT_ORIGINAL_FILE_NAME = new _tagpropertykey();
        WPD_OBJECT_ORIGINAL_FILE_NAME.fmtid = DeviceGUID.OriginalFileName;
        WPD_OBJECT_ORIGINAL_FILE_NAME.pid = 12;
        values.SetStringValue(WPD_OBJECT_ORIGINAL_FILE_NAME, Path.GetFileName(fileName));

        var WPD_OBJECT_NAME = new _tagpropertykey();
        WPD_OBJECT_NAME.fmtid = DeviceGUID.ObjectName;
        WPD_OBJECT_NAME.pid = 4;
        values.SetStringValue(WPD_OBJECT_NAME, Path.GetFileName(fileName));

        // my tests to set date modified
        // reading modified date returns this format: "yyyy/MM/dd:hh:mm:ss.000" (values.GetStringValue(property, out datemodified)
        var OBJECT_DATE_MODIFIED = new _tagpropertykey();
        OBJECT_DATE_MODIFIED.fmtid = WPD_OBJECT_DATE_MODIFIED.fmtid;
        OBJECT_DATE_MODIFIED.pid = WPD_OBJECT_DATE_MODIFIED.pid;
        // values.SetStringValue(OBJECT_DATE_MODIFIED, "2020/01/01:01:01:01.000");
        // value with StringValue produce runtime error
        values.SetUnsignedLargeIntegerValue(OBJECT_DATE_MODIFIED, (ulong)1316137362000);
        // value with UnsignedLargeInteger runs without error, but date modified ist not set 

        return values;
    }

// the Guid an pid for date modified
static class WPD_OBJECT_DATE_MODIFIED
    {
        // [ VT_DATE ] Indicates the date and time the object was modified on the device.
        // DEFINE_PROPERTYKEY( WPD_OBJECT_DATE_MODIFIED , 0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C , 19 ); 
        public static Guid fmtid = new Guid(0xEF6B490D, 0x5CD8, 0x437A, 0xAF, 0xFC, 0xDA, 0x8B, 0x60, 0xEE, 0x4A, 0x3C);
        public static uint pid = 19;
    }
How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *