how to get 32 bit application data folder from 64 bit application on 64 bit Machine using c# code

environment.specialfolder.programfiles returns x86
programw6432
programfiles(x86)
program files (x86)
program files (x86 windows 10)
program files vs program files (x86)
wow6432node registry redirection
wow6432node on 32 bit machine

I am facing very strange issue:

I got a 64 bit c#.net application on 64 bit Windows Server 2008 R2 machine and it is being invoked by a Windows Service and it is started under Local System User, Moreover,this 64 bit c#.net application launches 32 bit java application and this java application has application data folder to C:\Windows\SysWOW64\config\systemprofile\AppData. 64 bit c#.net application has app data folder to C:\Windows\System32\config\systemprofile\AppData

So for 32 bit application app data folder is (in case of Local system User):-C:\Windows\SysWOW64\config\systemprofile\AppData

and for 64 bit application app data folder is(in case of Local system User):-C:\Windows\System32\config\systemprofile\AppData

Please Note: this is not typing mistake that they refer to opposite folders(it is a decision by microsoft for 64 bit OS), you can read https://msdn.microsoft.com/en-us/library/aa384187.aspx for detailed explanation.

Now, I need to write few files to 32 bit app data folder from 64 bit application as these files would be used by 32 bit java application.

So, I need to know How I can get 32 bit app data folder from 64 bit application using c#.net.

Important Note: this issue would be faced when application is launched under local system user (i.e. application has been launch by window services) and there won't be any issue when a user explicitly launches the application beacause in this case,user app data folder would be same for 64 bit and 32 bit application.

You first check if you actually are running inside of a 64 bit process on a 64 bit OS. And if so, you construct the path yourself. Otherwise you can just retrieve the system path and append your target path.

String path;
//detect if the current application is 64 bit and running on a 64 bit system
//NOTE: needs .NET Framework 4 to work
if (Environment.Is64BitOperatingSystem && Environment.Is64BitProcess)
{
    path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "SysWOW64");
}
else
{
    path = Environment.GetFolderPath(Environment.SpecialFolder.System);
}
//append your target path
path = Path.Combine(path, @"config\systemprofile\AppData");

Please note that using EnvironmentIs64BitOperatingSystem and Environment.Is64BitProcess requires at least .NET-Framework 4.

Finding the ProgramFiles64 Folder in a 32 Bit App, Unfortunately getting the `Program Files` folder is a bit of problem due to in 32 bit mode, or have an application that can run either 32 or 64 bit, You can find Local App Data, Programs, My Documents, Pictures and so on using the On 64 bit machines, the Program Files location where applications� How can a 32-bit application properly get the location of the folder from 64-bit Windows Vista? Background. Our application has a service component which is supposed to launch other processes based on requests from user-session-specific component. The applications launched can be 32-bit or 64-bit.

The simplest solution is to rebuild the C# application as 32-bit, or to use 64-bit Java.

If you can't do either, create a 32-bit application that does nothing but look up the application data path, and run it from your C# application. The 32-bit application could be written in C, C#, or Java.

32-bit and 64-bit Application Data in the Registry, On 64-bit Windows, portions of the registry entries are stored separately for 32-bit application and 64-bit applications and mapped into separate� The following steps show how to access a 32-bit ODBC driver from a 64-bit application on a 64-bit Windows machine. The ODBC driver used is the Microsoft Access ODBC driver. The application used is the SQL Server Integration Services (SSIS) Import and Export Wizard.

Are you using "shortcut" keyword to point appdata directory, eg %APPDATA%? Could you use a direct path, eg @"C:\Users\%username%\AppData\Local"

Environment.SpecialFolder Enum (System), Specifies enumerated constants used to retrieve directory paths to system The directory that serves as a common repository for application-specific data for the To get the x86 common program files directory in a non-x86 process, use the example produces the following results: GetFolderPath: C:\WINNT\System32 */ Run 32-bit Windows-based applications seamlessly on 64-bit Windows with the WOW64 x86 emulator. Also learn about the registry director, file system redirector, application installation on 64-bit systems, and debugging WOW64.

This is the same answer as #2 above (via Sebastian Baumhekel), with some errors corrected. I am on Win 7 and have the same issue. As some have pointed out this may not be future OS safe... However it solves my current issue.

The issue is on 64bit Win7 when you run a 32bit Windows Service as Local System user and ask for this folder:

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

It returns the 64bit version (with System32):

C:\Windows\System32\config\systemprofile\AppData\

Main point of clarification (as OP states):

SysWOW64 gets 32bit application data...

System32 gets 64bit application data...

This from OP link:

https://docs.microsoft.com/en-us/windows/desktop/WinProg64/file-system-redirector

I just wasted 4 hours of my life getting to the bottom of this so I wanted to clarify!

    public 32bitWindowsServiceOn64bitWindows()
    { 
        // Note: this service is configured to run as "Local System" user...

        string appdata;

        // need to do this, because this runs as Local System user... 
        // which gets the wrong SpecialFolder.ApplicationData (ie, System32 for 64bit apps) should be SysWOW64 (for 32bit apps)

        if (Environment.Is64BitOperatingSystem && Environment.Is64BitProcess)
        {
            // the application is a 64 bit app
            appdata = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "System32");
        }
        else
        {
            // the application is a 32 bit app
            appdata = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "SysWOW64");
        }
        // Append your target path. Do you want Local or Roaming?
        appdata = Path.Combine(appdata, @"config\systemprofile\AppData\Roaming\MyApp");
        //appdata = Path.Combine(appdata, @"config\systemprofile\AppData\Local\MyApp");
        ...

How to view the system registry by using 64-bit versions of Windows, Many of the 32-bit keys have the same names as their 64-bit counterparts, and vice The WOW64 registry redirector presents 32-bit programs with different keys for the 32-bit version of Registry Editor in the %systemroot%\Syswow64 folder. To support the co-existence of 32-bit and 64-bit COM registration and program� Stupid question time: Aside from the obvious "It's the easiest way of telling which programs were installed as a 64-bit application," is there anything on an OS level that would stop me from installing a 32-bit application in the "Program Files" folder (as opposed to "Program Files (x86)") on a 64-bit OS? Hi. It's definitely not 'purely semantic'.

The 'Program Files (x86)' and 'SysWOW64' folders explained , In the coming years, more and more people will have a 64-bit computer with a 64 -bit On a 64-bit Windows, both 32-bit and 64-bit programs can be run Data files can usually placed in same folders and shared by both a 32 and 64 bit application. SysWOW64, 32, Windows System folder (system directory) for 32- bit files. Open the finder of notepad by “Ctrl +f”. Then type “PE”, if you find “L” next to “PE” this means that Program is 32 bit. If you find the letter “d” with arrow sign that means the Program is 64 bits. These are easy and simple ways to find whether your program is of 32 bit or 64 bit. You don’t need to install any third-party software as there are already many ways to check within your OS. I hope all these are easy to get and quick to check. Continue Reading:

Windows registry on 32 and 64 bit windows, HKEY_LOCAL_MACHINE/SOFTWARE/WOW6432NODE/ODBC/ ODBC.INI for 32-bit applications on 64-bit machines. This key contains a key for each Data� 'Program Files' Folder in Windows 10 32-bit. Therefore, if you find only one folder when checking this location, you're using a 32-bit version of Windows. If there are two "Program Files" folder, you're for sure using a 64-bit version.

Program Files, x86-64 and IA-64 versions of Windows have two folders for application files: The Program Files folder serves as the default installation target for 64-bit programs,� Given that the accepted answer is very complex. There are simpler ways. Mine is a variation of alexandrudicu's anaswer. Given that 64-bit windows install 32-bit applications in Program Files (x86) you can check if that folder exists, using environment variables (to make up for different localizations) e.g.

Comments
  • "it is a bug by microsoft for 64 bit OS" - it's not a bug, it's a deliberate design decision.
  • could be let me correct my statement
  • Why dont you use Environment.GetFolderPath(Environment.SpecialFolder.SystemX86) and Environment.GetFolderPath(Environment.SpecialFolder.System) for 32 bit and 64 bit app data path and then append the config/systemprofile/AppData ?
  • I use this line Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
  • This depends on an implementation detail, so it might break in future versions of Windows.
  • Indeed SysWOW64 is an implementation detail, but it won't change in the future - if you look at your System32 directory, you see why. It should be named System64 in 64 bit versions of Windows, but due to legacy compatibility it is not.
  • Unless you've got that in writing, you can't count on it. While the syswow64 folder is unlikely to be renamed or removed for the foreseeable future, there's no compelling reason that I'm aware of why the local system profile has to be inside that folder. Third-party software isn't supposed to use that folder anyway, and I'm not aware of any major software products that do, so backwards compatibility isn't likely to be a firm constraint in this case. Oh, the odds are in your favor, but IMO this sort of gamble is only appropriate if there is no other choice.
  • there is not anything else that I could do with code itself...Because, first two solution, I was aware and there were some reason. So, I could not go with them. Moreover, launching 32 bit application is that right?? because my application will be called by a wcf service and there would be so many instance of the application as it per call instance.
  • Since you're already running a separate instance of 32-bit Java for each call, running another 32-bit application first is unlikely to affect performance much. But you could cache the result if you wanted to streamline things. One way would be to put it in the registry; if you create a registry key with REG_OPTION_VOLATILE, it will disappear the next time the system is rebooted, which ensures that the cached data won't become invalid.
  • The local system account's application data folder isn't in C:\Users.