Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do get the path of Program Files regardless of the architecture of the target machine

I'm programming in C#/.NET. I want to be able to return the Program Files directory from the target machine no matter what the architecture of the target machine is. To clarify, I want it to return C (or whatever drive the OS is on):/Program Files no matter what bitness their version of Windows is.

I could just hardcode in the directory except if the user was running Windows that's not installed on the C: drive it wouldn't work.

I found

FileInfo(System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) 

but I think it returns the Program Files (x86) folder on a 64 bit windows machine.

like image 599
PICyourBrain Avatar asked Feb 17 '10 22:02

PICyourBrain


People also ask

What is C :\ Program files x86?

On Windows editions that support x86 emulation, there are two directories for program files. The C:\Program Files directory is for programs in the native system bitness, and the the C:\Program Files (x86) directory is for programs that run under the x86-32 emulator.

Why does my computer have Program Files and Program Files x86?

The Windows separated and created two different directories of the 32-bit program and 64-bit program. The Program Files stores all the 64-bit programs and the Program Files (x86) stores all the 32-bit programs. x86 stands for different processor types, i.e. 286, 386, 486, 586/Pentium.

What is the difference between Program Files and Program Files x86?

The regular Program Files folder holds 64-bit applications, while "Program Files (x86)" is used for 32-bit applications. Installing a 32-bit application in a PC with a 64-bit Windows automatically gets directed to Program Files (x86).


1 Answers

System.Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) returns "c:\Program Files" on a 64-bit machine, unless the code is build to target x86, in which case it returns "C:\Program Files (x86)", so I guess that would work for you.

like image 109
Fredrik Mörk Avatar answered Oct 13 '22 21:10

Fredrik Mörk