4

在(更新 ModifiedTime)WinRT 中“触摸”文件的优雅/高性能方式?

我有一些代码需要删除超过 30 天的文件。这很好用,但在某些情况下,我需要更新文件上的时间以重置 30 天窗口,并防止删除。在 basicProperties 列表中,ModifiedTime 是只读的,所以我需要找到另一种方法来更新它...

方法一:重命名两次

    // Ugly, and may have side-effects depending on what's 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); }
       );
    }

方法二:修改文件属性

    // 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 });
    }

方法 3:通过 P/Invoke 使用 Win32 API?

  • 不确定这是否适用于 ARM 设备?
  • 通过认证?
  • 表现出色?
  • 有没有最好的方法来做到这一点?代码示例?

有人有其他想法吗?我有点卡住了:-)

非常感谢,乔恩

4

2 回答 2

2

我只是需要这个,这是我的解决方案。

用法

await storageFileToTouch.TouchAsync();

代码

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();
        }
    }
}
于 2015-01-09T16:21:31.340 回答
1

假设您计划组合 RT 机器上本地存在的文件列表,而不是该云中的某个位置(否则我们不必担心 WinRT doc mod 过程),您可以轻松使用应用程序数据容器提供给每个应用程序以存储非常薄的数据(键值对非常适合)。

通过这种方式,您可以为每个需要持久化的文件存储一个未来的删除日期,以便下次删除它时,在删除过程发生之前,应用程序会检查应用程序存储数据。然后,当您只是想确保它们不会从您的进程中删除时,您无需担心正在迭代的文件的权限。

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
         }
    }
}

资源:

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

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

于 2012-10-12T14:20:02.623 回答