如何检索 system32 或 SysWOW64 的正确路径? [英] How to retrieve correct path of either system32 or SysWOW64?

查看:48
本文介绍了如何检索 system32 或 SysWOW64 的正确路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 32 位进程,可以在 32 位或 64 位 Windows 中运行.因此,很自然地,如果进程试图访问文件 c:windowssystem32file.ext,它会被重定向到 c:windowsSysWOW64file.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:windowssystem32file.ext, it would be redirected to c:windowsSysWOW64file.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?

推荐答案

以下代码将返回正确的系统目录(system32syswow64):

The following code will return the correct system directory (system32syswow64):

[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%System32 On X64 you'll get %windir%SysWow64

希望对你有帮助

这篇关于如何检索 system32 或 SysWOW64 的正确路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆