Search code examples
uwpstoragefile

How to set datemodified property for a Storage file in c# uwp?


I need to set specific time for date modified property, not current time.

I can't use FileInfo because the file is located in folder outside \AppData\Local\Packages<packagename>\LocalState (system.io has no access).


Solution

  • How to set datemodified property for a Storage file in c# uwp?

    UWP has SavePropertiesAsync method that could use to modify basic property, unfortunately, System.DateModified is Innate, so we can't update it manually in uwp platform as this answer said. For this scenario, we suggest update FileInfo use desktop extension, for more you could refer to stefan's blog UWP with Desktop Extension. And the other way is copy the file into app's local folder, and put it back after modified property

    The following is sample code that update LastWriteTime within desktop extension

    OpenFileDialog openFileDialog = new OpenFileDialog();
    if (openFileDialog.ShowDialog() == true)
    {
        FileInfo fi = new FileInfo(openFileDialog.FileName);
        fi.LastWriteTime = DateTime.Now;
    }