问题描述
我有一个32位进程,可以在32位或64位Windows中运行。因此,自然地,如果该进程尝试访问文件 c:\windows\system32\file.ext
,它将被重定向到 c:\windows\SysWOW64\file.ext
。到目前为止很好-我不想禁用重定向。
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.
我的问题是我的进程实际上没有访问 -相反,它只采用其路径并将其写入文本文件,而我希望该文本文件在64位系统上读取 SysWOW64
,以及在32位系统上的 system32
。我该怎么办?
My problem is that my process doesn't actually access the 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?
推荐答案
以下代码将返回正确的系统目录(system32\syswow64):
The following code will return the correct system directory (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();
}
在x86上,您将获得%windir%\System32
在X64上,您将获得%windir%\SysWow64
On x86 you'll get %windir%\System32On X64 you'll get %windir%\SysWow64
希望这会有所帮助
这篇关于如何获取system32或SysWOW64的正确路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!