An elegant / efficient way to “Touch” a file in (update ModifiedTime) WinRT?
I have a code that should delete files older than 30 days. This works well, but in some cases I need to update the time in the file to reset the 30-day window and prevent deletion. In the basicProperties list, ModifiedTime is read-only, so I need to find another way to update it ...
Method 1: rename twice
// Ugly, and may have side-effects depending on what using the file // Sometimes gives access denied... public static async Task TouchFileAsync(this StorageFile file) { var name = file.Name; await file.RenameAsync("~" + name).AsTask().ContinueWith( async (task) => { await file.RenameAsync(name); } ); }
Method 2: Change the file property
// Sometimes works, but currently throwing an ArgumentException for // me, and I have no idea why. Also tried many other properties: // http://msdn.microsoft.com/en-us/library/windows/desktop/bb760658(v=vs.85).aspx public static async Task TouchFileAsync(this StorageFile file) { var prop = new KeyValuePair<string, object>("System.Comment", DateTime.Now.Ticks.ToString()); await file.Properties.SavePropertiesAsync(new[] { prop }); }
Method 3: Use Win32 API via P / Invoke?
- Not sure if this will work on ARM devices?
- Skip certification?
- Be a performer?
- Is there a better way to do this? Code example?
Does anyone have any other ideas? I'm a little stuck :-)
Thanks a lot John
source share