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

时间:2012-09-26 14:33:25

标签: c# file-io windows-runtime storagefile

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

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

方法1:重命名两次

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

方法2:修改文件属性

    // 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设备?
  • 通过认证?
  • 表现好吗?
  • 有最佳方法吗?代码示例?

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

非常感谢, 乔恩

2 个答案:

答案 0 :(得分: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();
        }
    }
}

答案 1 :(得分: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/