xtempore
xtempore

Reputation: 5505

How can I get a consistent path for read/write application in C# apps?

I have a C# app that needs to be able to read and write files that will be available to all users.

I thought that this would give me an appropriate location to store these files:

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)

I have 2 apps that need to access the same folders. The main app is UWP and there is a utility app which is a console app. Running on Windows 10.

The main app returns this:

C:\Users\User\AppData\Local\Packages\ba131026-0d4a-4706-afec-3246196e7d60

The console app returns the much simpler:

C:\ProgramData

I don't mind which I use, but I just need them to be able to both access the same folder. Is there some method to always get the same folder from both types of app?

Upvotes: 2

Views: 628

Answers (1)

adjan
adjan

Reputation: 13652

UWP apps run in a sandbox, that's why Environment.SpecialFolder.CommonApplicationData doesn't point to the "regular" folder C:\ProgramData

I would advise against using CommonDocuments since that means giving the UWP app more permissions than needed, and it seems rather cumbersome (see this answer).

Instead, I would recommend accessing the UWP app's folder from within the console app, which always will be %LOCALAPPDATA%/Packages/AUMID, where AUMID is the UWP app identifier (Application User Model ID).

var aumid = "ba131026-0d4a-4706-afec-3246196e7d60"
var path = Path.Combine(
     Environment.GetFolderPath(
         Environment.SpecialFolder.LocalApplicationData), 
         $"/Packages/{aumid}"
     )
);

Upvotes: 2

Related Questions