Getting syswow64 directory using 32-bit application

陌路散爱 提交于 2020-01-01 08:10:06

问题


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


回答1:


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




回答2:


Using the SHGetSpecialFolderPath function:

[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




回答3:


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




回答4:


I had the same problem. The solutions is to set the "Platform target" as x86 instead of AnyCPU or x64 in project properties in Visual Studio. In this case the path will be "C:\Windows\system32" but it actually redirects 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));



来源:https://stackoverflow.com/questions/3540930/getting-syswow64-directory-using-32-bit-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!