Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment.SpecialFolder.ProgramFiles returns the wrong directory

Tags:

c#

I am working on a C# project and I am using the following code:

string rootPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);

However, when I look at the rootPath, it's set to C:\Program Files (x86).

Why would do it this as there is an Environment.SpecialFolder.ProgramFilesX86 which I would have thought would have returned the above.

like image 963
Boardy Avatar asked Sep 18 '25 22:09

Boardy


2 Answers

If your project is currently targeting the x86 platform, both of those enum values will return the Program Files(x86) directory.

Change the target platform for your project to x64, and SpecialFolder.ProgramFiles should return the Program Files directory instead.

like image 93
Grant Winney Avatar answered Sep 20 '25 14:09

Grant Winney


I have a scenario where i could not change the target of the executing assembly. So i use this method to get always the x64 (also when running in 32bit):

var programFilesX64 = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64)
.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion")?.GetValue("ProgramFilesDir");
like image 30
Suplanus Avatar answered Sep 20 '25 15:09

Suplanus