An elegant / efficient way to “Touch” a file in (update ModifiedTime) in WinRT?

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

+4
source share
2 answers

Assuming you plan to comb the list of files that exist locally on the RT machine and not somewhere in this cloud (otherwise we don’t have to worry about the process of creating a WinRT document), you can easily use the application data container provided for each application for storing very thin data (key value pairs are very good).

Thus, you save the date of future deletion for each file that must be saved, so the next time it was raised for deletion, the application checks the application storage data before the deletion process. Then you don’t need to worry about the file permissions that you execute when you are just trying to make sure that they are not removed from your process.

 Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings; // Create a setting in a container Windows.Storage.ApplicationDataContainer container = localSettings.CreateContainer("FilesToPersist", Windows.Storage.ApplicationDataCreateDisposition.Always); StorageFile file = fileYouWantToPersist; if (localSettings.Containers.ContainsKey("FilesToPersist")) { localSettings.Containers["FilesToPersist"].Values[file.FolderRelativeId] = DateTime.Now.AddDays(30); } // Read data from a setting in a container bool hasContainer = localSettings.Containers.ContainsKey("FilesToPersist"); bool hasSetting = false; if (hasContainer) { hasSetting = localSettings.Containers["FilesToPersist"].Values.ContainsKey(file.FolderRelativeId); if(hasSettings) { string dt = localSettings.Containers["FilesToPersist"].Values[file.FolderRelativeId]; if(Convert.ToDateTime(dt) < DateTime.Now) { //Delete the file } } } 

Resources

http://msdn.microsoft.com/en-us/library/windows/apps/windows.storage.applicationdata.aspx

http://lunarfrog.com/blog/2011/10/10/winrt-storage-accesscache/

+1
source

I just needed this, and here is my solution.

using

 await storageFileToTouch.TouchAsync(); 

code

 public static class StorageFileExtensions { /// <summary> /// Touches a file to update the DateModified property. /// </summary> public static async Task TouchAsync(this StorageFile file) { using (var touch = await file.OpenTransactedWriteAsync()) { await touch.CommitAsync(); } } } 
+1
source

Source: https://habr.com/ru/post/1436393/


All Articles