'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").
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"?
Solution 1:[1]
Here is the answer, in a Microsoft Learn course.
You can access the sandbox using the AppDataDirectory static property of the FileSystem class:
string path = FileSystem.AppDataDirectory;
Example. FileSystem.AppDataDirectory returns "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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
