简体   繁体   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

I have this code that creates, check the existence, deletes, etc a file:我有这段代码可以创建、检查是否存在、删除等文件:

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

When I run it, the file is not created in LocalApplicationData directory (in my case " C:\Users\userName\AppData\Local\ "), but in a different subdirectory that I found doing a search with the windows file explorer (in my case this subfolder was " C:\Users\userName\AppData\Local\Packages\F0C6F5FC-4B4B-478D-958D-BAD69252637E_9zz4h110yvjzm\LocalCache\Local ").当我运行它时,该文件不是在LocalApplicationData目录中创建的(在我的情况下为“ C:\Users\userName\AppData\Local\ ”),而是在我发现使用 Windows 文件资源管理器进行搜索的另一个子目录中(在我的情况是这个子文件夹是“ C:\Users\userName\AppData\Local\Packages\F0C6F5FC-4B4B-478D-958D-BAD69252637E_9zz4h110yvjzm\LocalCache\Local ”)。

The program runs correctly, but How can I get this real directory where File.Create(), File.Exists() and File.Delete() are interacting with "MyFile.txt"?程序运行正常,但我怎样才能获得 File.Create()、File.Exists() 和 File.Delete() 与“MyFile.txt”交互的真实目录

Here is the answer, in a Microsoft Learn course .这是Microsoft Learn 课程中的答案。

You can access the sandbox using the AppDataDirectory static property of the FileSystem class:您可以使用 FileSystem 类的AppDataDirectory静态属性访问沙箱:

string path = FileSystem.AppDataDirectory;

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

This is the right place to store local data in a device.这是在设备中存储本地数据的正确位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM