I have an application compiled in x86 mode (in c#) from which I need to access a certain file that exists in the 64-bit program files folder (of a 64-bit Windows of course).
I don't want to just hardcode C:\Program Files
as a string in my application because a few target computers may have Windows installed in a different drive, or may be in another languages.
The problem I'm encountering is that using Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)
returns the x86 flavor instead of the desired directory, unless I compile my program in 64-bit mode. Out of curiosity, what can I do to avoid doing such?
On a 64-bit version of Windows, 64-bit programs are stored in the “C:\Program Files” folder and 32-bit programs are stored in the “C:\Program Files (x86)” folder.
The regular Program Files folder holds 64-bit applications, while "Program Files (x86)" is used for 32-bit applications.
The 64-bit versions of Windows don't provide support for 16-bit binaries or 32-bit drivers. Programs that depend on 16-bit binaries or 32-bit drivers can't run on the 64-bit versions of Windows unless the program manufacturer provides an update for the program.
Hello padora, The difference is that the Program Files (x86) is for 32 Bit applications. The Program Files is for programs that support 64 Bit fully. No you can't install one under the other.
There was an option called "prefer 32-bit" in the project properties. Unchecking that option did the trick. I'm still interested in a code solution instead of this, nevertheless.
I actually think disabling Prefer 32bit on the build options is the better way to go. If you don't want your program to be treated like a 32 bit process, why not make it a 64 bit process and save yourself some trouble.
See also this article on the subject by Raymond Chen.
Having said that, the ProgramW6432
environment variable suggested by griddoor worked fine for me when I tried it.
I'm afraid that the WinAPI does not support what you require. Due to virtualization it is not possible for a 32bit application to get the path to 64bit directories.
Refer to: https://social.msdn.microsoft.com/Forums/vstudio/en-US/37e798f5-1b9b-42ce-89af-486ee3531c0b/32-bit-app-how-to-get-cprogram-files-directory-using-environmentgetfolderpath?forum=csharpgeneral
Any attempt to "guess" the right path or use the registry could cause an issue in the future...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With