【问题标题】:How lock a file during write so that it cannot be read while open?如何在写入期间锁定文件以使其在打开时无法读取?
【发布时间】:2017-07-26 22:13:22
【问题描述】:

在过去,我会在打开文件时为文件设置共享模式。 UWP有没有类似的概念?似乎没有什么明显的。

【问题讨论】:

  • 您始终可以使用CreateFile2 打开文件,请求0dwShareMode(即不共享)。

标签: uwp


【解决方案1】:

FileShare 类属于 .net API,但在 uwp 应用程序中也支持。 System.IO命名空间有很多支持uwp应用的API,详细可以参考System.IO namespaces for UWP apps。所以可以使用FileStream读写文件,将FileShare设置为none会导致文件无法读取。例如:

StorageFolder localfolder = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile samplefile = await localfolder.GetFileAsync("sample.txt");
FileStream stream = new FileStream(samplefile.Path, FileMode.Open, FileAccess.Read, FileShare.None);

【讨论】:

    猜你喜欢
    • 2019-12-20
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    相关资源
    最近更新 更多