Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
377 views
in Technique[技术] by (71.8m points)

c# - 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:WindowsSystem32 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:inDebugsomexe.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)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...