如何获取程序文件x86 env变量?

Ash*_*hok 104 windows environment-variables windows-7-x64

我想知道如何在命令提示符下显示Program Files(x86)的位置.我正在使用Windows 7 64位.

我试过了:

echo %programfiles(x86)%并且echo %programfiles%,
两者都只显示C:\Program Files

当我手工检查注册表,
HKLM /软件/微软/窗/ CURRENTVERSION,
programfilesdirC:\Program Files

HKLM /软件/ WOW64 /微软/的Winodws/CURRENTVERSION,
programfilesdirC:\Program Files (x86).

但是,为什么我总是用C:\ Program Files?

Sec*_*att 169

在以64位模式运行的64位计算机上:

  • echo %programfiles% ==> C:\Program Files
  • echo %programfiles(x86)% ==> C:\Program Files (x86)

在以32位(WOW64)模式运行的64位计算机上:

  • echo %programfiles% ==> C:\Program Files (x86)
  • echo %programfiles(x86)% ==> C:\Program Files (x86)

在以32位模式运行的32位计算机上:

  • echo %programfiles% ==> C:\Program Files
  • echo %programfiles(x86)% ==> %programfiles(x86)%

  • 什么%programfiles(x86)%将在32位模式下在32位机器上返回? (18认同)
  • 可能值得将%ProgramW6432%添加到上面的列表中. (3认同)
  • 这同样适用于 32 位版本的 Windows 7 - 没有 %programfiles(x86)% 环境变量 (3认同)
  • 我第二@Alex Wiese,%ProgramW6432% 将始终拥有 64 位程序文件目录。%ProgramFiles(x86)% 将始终具有 32 位程序文件目录。这两个变量是稳定的...其他变量可能会根据 64 位操作系统(或命令提示符)运行的模式而变化。 (2认同)

Vin*_*ini 32

另一个相关的环境变量是:

%ProgramW6432%

因此,在以32位(WOW64)模式运行的64位计算机上:

  • echo%programfiles%==> C:\ Program Files(x86)
  • echo%programfiles(x86)%==> C:\ Program Files(x86)
  • echo%ProgramW6432%==> C:\ Program Files

来自维基百科:

%ProgramFiles%变量指向Program Files目录,该目录存储Windows和其他所有已安装的程序.英语系统的默认设置是"C:\ Program Files".在64位版本的Windows(XP,2003,Vista)中,还有%ProgramFiles(x86)%,默认为"C:\ Program Files(x86)"和%ProgramW6432%,默认为"C:\Program Files".%ProgramFiles%本身取决于请求环境变量的进程本身是32位还是64位(这是由Windows-on-Windows 64位重定向引起的).

参考:http://en.wikipedia.org/wiki/Environment_variable

  • 更好的参考:[MSDN:WOW64实现细节](https://msdn.microsoft.com/zh-cn/library/aa384274)-“从Windows 7和Windows Server 2008 R2开始添加了ProgramW6432和CommonProgramW6432环境变量。” 维基百科直接与此矛盾。有趣的是,根据MSDN,维基百科仅列出了三个不支持该变量的版本。不幸的是,我没有要测试的64位XP / Vista。 (2认同)

小智 5

在 Windows 64 位机器上, echo %programfiles(x86)% 确实打印 C:\Program Files (x86)


Geo*_*son 5

在 64 位 Windows 系统上,各种环境变量和某些 Windows 注册表项的读取会重定向到不同的源,具体取决于执行读取的进程是 32 位还是 64 位。

下表列出了这些数据源:

X = HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion
Y = HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion
Z = HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
     
READING ENVIRONMENT VARIABLES:    Source for 64-bit process               Source for 32-bit process
-------------------------------|----------------------------------------|--------------------------------------------------------------
                %ProgramFiles% :  X\ProgramW6432Dir                       X\ProgramFilesDir (x86)
           %ProgramFiles(x86)% :  X\ProgramFilesDir (x86)                 X\ProgramFilesDir (x86)
                %ProgramW6432% :  X\ProgramW6432Dir                       X\ProgramW6432Dir
     
          %CommonProgramFiles% :  X\CommonW6432Dir                        X\CommonFilesDir (x86)
     %CommonProgramFiles(x86)% :  X\CommonFilesDir (x86)                  X\CommonFilesDir (x86)
          %CommonProgramW6432% :  X\CommonW6432Dir                        X\CommonW6432Dir
     
                 %ProgramData% :  Z\ProgramData                           Z\ProgramData


      READING REGISTRY VALUES:    Source for 64-bit process               Source for 32-bit process
-------------------------------|----------------------------------------|--------------------------------------------------------------
             X\ProgramFilesDir :  X\ProgramFilesDir                       Y\ProgramFilesDir
       X\ProgramFilesDir (x86) :  X\ProgramFilesDir (x86)                 Y\ProgramFilesDir (x86)
            X\ProgramFilesPath :  X\ProgramFilesPath = %ProgramFiles%     Y\ProgramFilesPath = %ProgramFiles(x86)%
             X\ProgramW6432Dir :  X\ProgramW6432Dir                       Y\ProgramW6432Dir
     
              X\CommonFilesDir :  X\CommonFilesDir                        Y\CommonFilesDir
        X\CommonFilesDir (x86) :  X\CommonFilesDir (x86)                  Y\CommonFilesDir (x86)
              X\CommonW6432Dir :  X\CommonW6432Dir                        Y\CommonW6432Dir
     
Run Code Online (Sandbox Code Playgroud)

例如,对于 32 位进程,环境变量的数据源%ProgramFiles%%ProgramFiles(x86)%注册表值HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir (x86)

但是,对于 64 位进程,环境变量的数据源%ProgramFiles%是注册表值HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramW6432Dir...而环境变量的数据源%ProgramFiles(x86)%是注册表值HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir (x86)

大多数默认的 Windows 安装都会将类似的字符串C:\Program Files (x86)放入注册表值中HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesDir (x86),但可以更改此值(以及其他值)。

在这些 Windows 注册表值中输入的任何内容都将在登录时由 Windows 资源管理器读取到相应的环境变量中,然后复制到随后生成的任何子进程。

注册表值HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramFilesPath尤其值得注意,因为大多数 Windows 安装都会将字符串放入%ProgramFiles%其中,以供 64 位进程读取。该字符串引用环境变量%ProgramFiles%,该环境变量又从注册表值获取数据HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\ProgramW6432Dir...除非某些程序先验更改了该环境变量的值。

我编写了一个小实用程序,它显示 64 位和 32 位进程的这些环境变量。你可以在这里下载。
包含 VisualStudio 2017 的源代码,编译后的 64 位和 32 位二进制可执行文件分别位于目录..\x64\Release和中..\x86\Release