[英]How can I open a specific page in my app when the user clicks a link from an outside source?
[英]How to lock open StorageFile to prevent user from renaming it outside of my app?
我有一個在 Windows 10 桌面上運行的基本 UWP 應用程序。 該應用程序定義了自定義文件格式,並允許用戶打開和編輯文檔。
我使用FileOpenPicker
讓用戶選擇文件。
我的問題是:如何防止用戶在我的應用程序中打開文檔時重命名/移動/刪除我的應用程序之外的文檔(例如在 Windows 資源管理器中)?
Word Mobile 鎖定打開的文檔。 如果我嘗試在 Windows 資源管理器中重命名打開的文檔,則會收到錯誤消息:
“無法完成操作,因為文件在 Runtime Broker 中打開。關閉文件並重試。”
我想達到同樣的目標。 我懷疑 Word Mobile 應用程序正在以特殊模式打開文件或以某種方式鎖定它。 我嘗試了各種Storagefile.OpenXXX()
方法,但沒有運氣:
// None of these seem to exclusively lock the file:
stream = await file.OpenStreamForWriteAsync();
stream = await file.OpenTransactedWriteAsync();
stream = await file.OpenAsync(FileAccessMode.ReadWrite);
stream = await file.OpenReadAsync();
我相信這應該是您正在尋找的電話:
file.OpenAsync(FileAccessMode.Read, StorageOpenOptions.AllowOnlyReaders);
來自: https : //docs.microsoft.com/en-us/uwp/api/windows.storage.storageopenoptions?view=winrt-19041
您可以在文件上設置共享模式,如果您將其設置為“無”,那么只要您保持打開狀態,其他人就無法打開或修改此文件:
new FileStream(name, FileMode.Open, FileAccess.Read, FileShare.Read);
這將使用 System.IO,您應該可以使用它。 完整文檔在這里: https : //msdn.microsoft.com/en-us/library/system.io.fileshare(v=vs.110).aspx
這對你有幫助嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.