Search code examples
c#win-universal-apponedrive

How to change created date of file on onedrive?


How to change created date in C# Onedrive SDK?

string[] _scopes = { "onedrive.readwrite", "onedrive.appfolder" };
IOneDriveClient _OneDriveClient = OneDriveClientExtensions.GetUniversalClient(_scopes);
await _OneDriveClient.AuthenticateAsync();

Item item = await _OneDriveClient.Drive.Special.AppRoot.Children["mydb.db"].Request().GetAsync();
Item newitem = new Item { CreatedDateTime = new DateTimeOffset(2013, 2, 15, 2, 3, 4, 0, new TimeSpan()) };
var ri = await _OneDriveClient.Drive.Items[item.Id].Request().UpdateAsync(newitem);

Last line throws accessdenied exception. What is wrong here?


Solution

  • You can't set the createdDateTime or modifiedDateTime properties as they're read-only system fields. However, you can set the values in the fileSystemInfo facet to keep track of alternate timestamps. Be careful though, as these are the timestamps that are used by the sync client when manifesting the item locally, and so arbitrarily changing them can result in your synced files having their timestamps adjusted.

    Item newitem = new Item
    {
      FilesystemInfo = new FileSystemInfo
      {
        CreatedDateTime = new DateTimeOffset(2013, 2, 15, 2, 3, 4, 0, new TimeSpan()),
      },
    };