views:

219

answers:

3

I'm trying to find a file inside the system directory. The problem is that when using

Environment.SystemDirectory

On a x64 machine, i'm still getting the System32 directory, instead of the Systemwow64 directory.

I need to get the "System32" directory on x86 machines, and "SystemWow64" directory on x64

Any ideas?

EDIT: To find the SysWow64 i'm using the "GetSystemWow64Directory". (more information here: pinvoke Notice that on non-x64 machines - result is '0'. Hope this helps someone

EDIT(2): Using the SHGetSpecialFolderPath:

[DllImport("shell32.dll")]
public static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner, [Out]StringBuilder lpszPath, int nFolder, bool fCreate);

string GetSystemDirectory()
{
    StringBuilder path = new StringBuilder(260);
    SHGetSpecialFolderPath(IntPtr.Zero,path,0x0029,false);
    return path.ToString()
}

Will return System32 on x86, and SysWow64 on x64
A: 

Use Environment.GetFolderPath(Environment.SpecialFolder.SystemX86) instead.

Chris Schmich
I don't have the SystemX86 in my enum (only "system", which returns the system32 folder).I'm under .net 2.0
Nissim
If you're getting "System32" in a 32-bit process on a 64-bit machine, it sounds like you have Windows' file system redirection (http://msdn.microsoft.com/en-us/library/aa384187(VS.85).aspx) turned off. Could that be the case?You can call `Wow64EnableWow64FsRedirection` to ensure that it's enabled (see http://msdn.microsoft.com/en-us/library/aa365744(VS.85).aspx).
Chris Schmich
+1  A: 

What your 32-bit program thinks is System32 is really SysWOW64 - don't code 32-bit apps to have any explicit knowledge of 64-bit, that's what WOW64 redirection is for

Paul Betts
Exactly. I just want to get the default system directory, but I'm always getting the system32 directory
Nissim
@Nissim: System32 is the default directory. It is just a different place in 32bit vs 64bit processes. Perhaps if you said *why* you need this information we might be able to help more (i.e. expand your question).
Richard
I need to get the IIS exe in order to determine the IIS version.I'm doing this by locating the file, and retrieving it's information through FileVersionInfo.But when i'm using Path.Combine(Environment.SystemDirectory, @"inetsrv\inetinfo.exe") I get FileNotFoundException.This is not a critical issue, since if this test fails, i'm retrieving this information through other sources, but still...
Nissim
You keep saying you want SysWOW64, but I think that's where you believe the 64-bit modules reside. The Sys-WOW64 directory is where the 32-bit modules are. You want the (Real) System32 folder, where the 64-bit modules are. For that, try %SystemRoot%\SysNative.
Paul Betts
A: 

The answer is described in my post (edit(2))

Nissim