Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get 64-bit "program files" directory in 32-bit Application

Tags:

c#

.net

directory

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?

like image 491
first timer Avatar asked May 02 '15 23:05

first timer


People also ask

Where is a user of 64-bit Windows stores 32-bit programs applications?

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.

Which Program Files folder is 64-bit?

The regular Program Files folder holds 64-bit applications, while "Program Files (x86)" is used for 32-bit applications.

Can a 32 bit application launch a 64-bit application?

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.

Can you put 64-bit in Program Files x86?

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.


Video Answer


2 Answers

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.

like image 128
Nacimota Avatar answered Sep 18 '22 07:09

Nacimota


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...

like image 44
Simon Mattes Avatar answered Sep 19 '22 07:09

Simon Mattes