Environment.SpecialFolder.CommonApplicationData returning Program Files/App folder

8k views Asked by At

I developed WPF application (.NET 4.5) which is storing some config files at CommonApplicationData folder.

For that purpose I'm using following:

string AppDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);

Also I'm appending Company name / product folder to that path and in 99.9% everything works. Application is able to write config there and to read it. App is mostly used on Win7 and Win8 machines but I got report from client that one machine is having some errors (Windows 7 Pro, .Net 4.5).

When I tried to debug the problem I found following in the log:

System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files (x86)\CompanyName\App\X\Y\Z\settings.config'.

Where

X\Y\Z\settings.config

should be part of C:\ProgramData but somehow application is returning folder where the app is installed (Program Files). I tried to replicate this on multiple machines and enviroments but with no luck.

My questions:

Is it possible to alter Environment.SpecialFolder.CommonApplicationData location in the Windows? Is this some security issue (group policy)? Maybe someone altered the path somehow.

I don't know why app in this one case is returning different folder than C:\ProgramData

If it is possible where I can check what is value of Environment.SpecialFolder.CommonApplicationData except programmatic?

Any advice or solutions?

I always can do some if checks and try to see if the path is wrong than manually correct it but it doesn't sound good to do that.

I always thought that using Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) will give me the right folder independent of the OS version.

I tried running app with full admin rights (plus run as administrator), the problem is same. Application is getting wrong path from Environment.SpecialFolder.CommonApplicationData.

Thank you in advance!

1

There are 1 answers

1
Mike On

This article explains where in the registry you can check to see if a user environment variable has been set for this enumeration.

https://msdn.microsoft.com/en-us/library/system.environmentvariabletarget(v=vs.110).aspx