如何在 c# uwp 中为存储文件设置 datemodified 属性?

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

我需要为修改日期设置具体时间 属性,而不是当前时间。

我无法使用 FileInfo,因为该文件位于 \AppData\Local\Packages\LocalState 之外的文件夹中(system.io 没有访问权限)。

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

UWP 有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 说。对于这种情况,我们建议更新 FileInfo 使用桌面扩展,更多信息您可以参考 stefan 的博客 UWP with Desktop Extension。另一种方式是将文件复制到app的本地文件夹中,修改后放回去属性

以下是在桌面扩展中更新 LastWriteTime 的示例代码

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