File getting copied to SysWOW64 instead of System32

I have to copy a psTool utility to System32 folder when my application runs.
I am on 64 bit Windows 7 and whenever, I try to copy the exe to system32 bit folder through File.Copy, the exe always gets copied to SysWow64 instead.

When I put a breakpoint on destFile, the path is shown as C:\Windows\System32 but the file does not go in there (goes to sysWow64). I have tried the Special Folder SystemX86, but the file again goes to sysWOW64.

string sourceFile = "C:\bin\Debug\somexe.exe"
string destFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), utilityName);
File.Copy(sourceFile, destFile, true);

Any suggestions what I am missing here?

EDIT

As pointed out below in the answer, there's file system redirection taking place. I am developing the app with the default settings of Visual Studio for a console application on a 64 bit OS. I am not sure what settings/switches have to be kept while compiling, so that the application works both on 32 bit and 64 bit OS.
Basically, it should just set copy the file to System32 only regardless of what bit OS it is.
Later in the program, I have to access the psTools utility through command line which is not available if I place it in SysWOW64. If I make change, to use SysWOW64's 32 bit cmd.exe, this would again be something 64 bit platform specific, which I do not want to opt.

Any solution which can have the app running both on 32-bit and 64 bit without an problems? Do I have to modify the code (how?) or do I have to modify some properties of this console application project (which properties)?


You've run afoul of file system redirection.

Because %windir%\System32 is reserved exclusively for 64-bit applications, on 64-bit versions of Windows, 32-bit applications that attempt to access the %windir%\System32 directory are automatically and transparent redirected to the 32-bit %windir%\SysWOW64 directory.

First, make sure that your program actually does belong in the 64-bit system folder. Windows does this automatic redirection for a reason. 32-bit stuff does not go in the %windir%\System32 folder on 64-bit versions of Windows.

If you're certain that you want to be copying stuff into the 64-bit system directory, you have a couple of options. The easiest is probably to just compile your utility as a 64-bit application. Alternatively, you can tell the WOW64 redirector that you know what you're doing and not to perform the redirection by using %windir%\Sysnative instead of %windir%\System32.


I had the same problem. The solutions is to set the "Platform target" as x64 or AnyCPU instead of x86 in project properties in Visual Studio. In this case the path will be "C:\Windows\system32" and will not redirect to "C:\Windows\SysWOW64" You can check this by placing any file in the "C:\Windows\SysWOW64" folder and then use File.Exists command to check if file is found in that folder:

File.Exists(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), sFileName));

Or

File.Exists(Path.Combine(Environment.SystemDirectory, sFileName));

enter image description here


I use a helper property in my 32 bit applications that always return the native system32 folder. The helper property is:

public static string NativeSystemPath
{
    get
    {
        if (Environment.Is64BitOperatingSystem)
        {
            return System.IO.Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.Windows),
                "Sysnative");
        }
        return Environment.GetFolderPath(Environment.SpecialFolder.System);
    }
}