Boardy
Boardy

Reputation: 36237

Environment.SpecialFolder.ProgramFiles returns the wrong directory

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.

Upvotes: 4

Views: 6364

Answers (2)

Suplanus
Suplanus

Reputation: 1601

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");

Upvotes: 6

Grant Winney
Grant Winney

Reputation: 66499

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.

Upvotes: 11

Related Questions