Home > Enterprise >  How can I get folder name under AppData/Local/Packages in MAUI?
How can I get folder name under AppData/Local/Packages in MAUI?

Time:09-19

I'm making a simple git tool use MAUI.

When I use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

I get "C:/Users/{MyName}/AppData/Roaming".

And then I want to save some file at Roaming/{MyAppName}/{Files};

But Windows will redirects this path to "C:/Users/{MyName}/AppData/Local/Packages/{GUID}_{hash}/LocalCache/Roaming/{MyAppName}/{Files}";

Now, I want to process git to do something with my files in myApp, but I don't kown how to get the redirected path(Package/{GUID}_{hash}/LocalCache/Roaming).

The {GUID}_{hash} in my computer is like that 5DDBD798-8CC0-490D-A71B-C2F900533A12_9zz4h110yvjzm;

I have learned from another question that the hash code is generated from the package and publisher identify information in the application package manifest.I cannot change or set it directly and code should not rely on its value.

So it's obviously not a good solution to hard code the path like what I do now:

private string _localRepoPath = "C:/Users/MyName/AppData/Local/Packages/5DDBD798-8CC0-490D-A71B-C2F900533A12_9zz4h110yvjzm/LocalCache/Roaming/MyTool/Files";

Summary:

  1. How to get {GUID}_{hash} use C# code.
  2. I saw many Folders in Packages/ are start with AppName not GUID, how can I use my AppName as the folder name. Like these: Microsoft.OneDriveSync_8wekyb3d8bbwe Microsoft.OneDriveSync_8wekyb3d8bbwe Microsoft.BingWeather_8wekyb3d8bbwe

CodePudding user response:

For the maui, you can try to use the File System Helper instead of the Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

var path = FileSystem.Current.CacheDirectory;
// the value of path is C:/Users/{MyName}/AppData/Local/Packages/{GUID}_{hash}/LocalCache/

var path = FileSystem.Current.AppDataDirectory;
// the value of path is C:/Users/{MyName}/AppData/Local/Packages/{GUID}_{hash}/LocalState/

For more information, you can check the official document.

  • Related