簡體   English   中英

如何獲取 .net File.Create() 放置文件的真實目錄? - Win11; VS2022 預覽; netMaui 應用程序

[英]How to get the real directory where .net File.Create() puts the file? - Win11; VS2022 preView; netMaui App

我有這段代碼可以創建、檢查是否存在、刪除等文件:

string _myFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "MyFile.txt");
Debug.Print(_myFilePath);// C:\Users\leode\AppData\Local\MyFile.txt
Debug.Print(File.Exists(_myFilePath).ToString());// False
File.Create(_myFilePath).Close();
Debug.Print(File.Exists(_myFilePath).ToString());// True
File.Delete(_myFilePath);
Debug.Print(File.Exists(_myFilePath).ToString());// False

當我運行它時,該文件不是在LocalApplicationData目錄中創建的(在我的情況下為“ C:\Users\userName\AppData\Local\ ”),而是在我發現使用 Windows 文件資源管理器進行搜索的另一個子目錄中(在我的情況是這個子文件夾是“ C:\Users\userName\AppData\Local\Packages\F0C6F5FC-4B4B-478D-958D-BAD69252637E_9zz4h110yvjzm\LocalCache\Local ”)。

程序運行正常,但我怎樣才能獲得 File.Create()、File.Exists() 和 File.Delete() 與“MyFile.txt”交互的真實目錄

這是Microsoft Learn 課程中的答案。

您可以使用 FileSystem 類的AppDataDirectory靜態屬性訪問沙箱:

string path = FileSystem.AppDataDirectory;

例子。 FileSystem.AppDataDirectory返回“C:\Users\{UserName}\AppData\Local\Packages\8BD0CA74-EE25-4D2F-8CF1-FCA28BBCD548_9zz4h110yvjzm\LocalState”

這是在設備中存儲本地數據的正確位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM