windows 如何检索 system32 或 SysWOW64 的正确路径?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3094520/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to retrieve correct path of either system32 or SysWOW64?
提问by Yodan Tauber
I have a 32-bit process that can run either in 32-bit or 64-bit Windows. So, naturally, if the process tried to access the file c:\windows\system32\file.ext
, it would be redirected to c:\windows\SysWOW64\file.ext
. So far so good - I don't want to disable the redirection.
我有一个可以在 32 位或 64 位 Windows 中运行的 32 位进程。因此,很自然地,如果该进程尝试访问该文件c:\windows\system32\file.ext
,它将被重定向到c:\windows\SysWOW64\file.ext
. 到目前为止一切顺利 - 我不想禁用重定向。
My problem is that my process doesn't actually accessthe file - instead it just takes its path and writes it into a text file, and I want that text file to read SysWOW64
on a 64-bit system, and system32
on a 32-bit system. How can I do that?
我的问题是我的进程实际上并没有访问该文件 - 相反,它只是获取它的路径并将其写入一个文本文件,我希望该文本文件SysWOW64
在 64 位系统和system32
32 位系统上读取. 我怎样才能做到这一点?
回答by Nissim
The following code will return the correct system directory (system32\syswow64):
以下代码将返回正确的系统目录(system32\syswow64):
[DllImport("shell32.dll")]
public static extern bool SHGetSpecialFolderPath(
IntPtr hwndOwner, [Out]StringBuilder lpszPath, int nFolder, bool fCreate
);
public static string GetSystemDirectory()
{
StringBuilder path = new StringBuilder(260);
NativeMethods.SHGetSpecialFolderPath(IntPtr.Zero, path, 0x0029, false);
return path.ToString();
}
On x86 you'll get %windir%\System32 On X64 you'll get %windir%\SysWow64
在 x86 上你会得到 %windir%\System32 在 X64 你会得到 %windir%\SysWow64
Hope this is helpful
希望这有帮助
回答by Vagaus
if I understood it correctly, you can use SHGetSpecialFolderPathpassing CSIDL_SYSTEMX86 to the csidl parameter. The documentation for the valid csidl'sstates that a 32 bit process will get:
如果我理解正确,您可以使用SHGetSpecialFolderPath将 CSIDL_SYSTEMX86 传递给 csidl 参数。有效csidl 的文档指出 32 位进程将获得:
- %windir%\system32on a 32 bits OS
- %windir%\syswow64on a 64 bits OS
- 32 位操作系统上的%windir%\system32
- %windir%\syswow64在 64 位操作系统上
Best regards
此致
回答by user1970413
System32 C:\Windows\System32 Windows System folder (system directory) for 64-bit files SysWOW64 C:\Windows\SysWOW64 Windows System folder (system directory) for 32-bit files Program Files C:\Program Files Folder for 64-bit program files Program Files (x86) C:\Program Files (x86) Folder for 32-bit program files
System32 C:\Windows\System32 Windows 64 位文件的系统文件夹(系统目录) SysWOW64 C:\Windows\SysWOW64 Windows 32 位文件的系统文件夹(系统目录) Program Files C:\Program Files 文件夹 64 位程序文件 Program Files (x86) C:\Program Files (x86) 32 位程序文件的文件夹