在启动应用程序时,Windows命令提示符是否在除PATH变量指定的位置之外的某处搜索?

时间:2023-02-10 08:07:12

I tried the following experiment.

我尝试了以下实验。

Before I start, I checked the PATH variable from cmd, which has the following value:

在开始之前,我检查了cmd中的PATH变量,该变量具有以下值:

Path=C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SYSTEMROOT%\System32\WindowsPowerShell\v1.0\;C:\ProgramData\Lenovo\ReadyApps;C:\Program Files\Microsoft SQL Server\110\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files (x86)\Windows Live\Shared;C:\Program Files (x86)\Skype\Phone\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\Calibre2\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;

At first, I thought that cmd only looks for executables in the directories contained in the PATH variable, so I randomly picked an application - winword.exe (Microsoft Word), and tried to launch it from the command line:

起初,我认为cmd只查找PATH变量中包含的目录中的可执行文件,因此我随机选择了一个应用程序 - winword.exe(Microsoft Word),并尝试从命令行启动它:

start winword

But to my surprise, the program launches! The reason I'm surprised is because I've searched through all the directories in the PATH variable for the exe file called 'winword' but all my searches came up empty!

但令我惊讶的是,该计划启动了!我感到惊讶的原因是因为我搜索了PATH变量中所有名为'winword'的exe文件的目录,但我的所有搜索都是空的!

I've therefore concluded that the command prompt must have known to search in places other than those specified in the PATH variable to look for executables.

因此,我得出结论,命令提示符必须已知在PATH变量中指定的位置以外的位置搜索以查找可执行文件。

So obviously, the next thing I did was to look for the precise location where the 'winword' executable file is located. It turns out that winword.exe is located here:

很明显,我接下来要做的是寻找'winword'可执行文件所在的精确位置。事实证明,winword.exe位于:

C:\Program Files\Microsoft Office 15\root\office15

Hence giving me the idea that maybe CMD automatically looks through ProgramFiles and ProgramFiles(x86) (and all their subdirectories) when executing the 'start' command? Which led to me trying to launch another application installed on my computer, Audacity, with the exe file located at:

因此,给我的想法是,在执行'start'命令时,CMD可能会自动查看ProgramFiles和ProgramFiles(x86)(及其所有子目录)?这导致我尝试启动安装在我的计算机Audacity上的另一个应用程序,其exe文件位于:

C:\Program Files (x86)\Audacity

Again, to my surprise, Audacity failed to launch when I typed:

令我惊讶的是,当我输入时Audacity未能启动:

start audacity

at the command line.

在命令行。

在启动应用程序时,Windows命令提示符是否在除PATH变量指定的位置之外的某处搜索?

I've then added the directory containing audacity.exe to PATH:

然后我将包含audacity.exe的目录添加到PATH:

set path=%path%;C:\Program Files (x86)\Audacity

after which i tried launching audacity again:

之后我再次尝试启动大胆:

start audacity

Well, not surprisingly, Audacity launched.

嗯,毫不奇怪,Audacity推出。

What I want to know is where exactly does the command prompt look for executables? Why is it that winword.exe launches even when the directory containing it not part of PATH, but the same thing isn't true for audacity.exe?

我想知道的是命令提示符到底在哪里寻找可执行文件?为什么即使包含它的目录不是PATH的一部分,winword.exe也会启动,但同样的事情对于audacity.exe也不是这样?

I tried other applications too. Chrome and Firefox works when I use the start command.

我也尝试了其他应用程序。当我使用start命令时,Chrome和Firefox可以正常工作。

UPDATE: I am running Windows version 6.3.9600 (Windows 8.1)

更新:我正在运行Windows版本6.3.9600(Windows 8.1)

4 个解决方案

#1


30  

At first, I thought that cmd only looks for executables in the directories contained in the PATH variable, so I randomly picked an application - winword.exe (Microsoft Word) and tried to launch it from the command line:

起初,我认为cmd只查找PATH变量中包含的目录中的可执行文件,因此我随机选择了一个应用程序 - winword.exe(Microsoft Word)并尝试从命令行启动它:

The reason winword.exe worked is that a registry key exists which defined the path to Microsoft Word (Winword.exe). A similar key exists for Firefox.exe and Chrome.exe if those applications are installed.

winword.exe工作的原因是存在一个注册表项,它定义了Microsoft Word(Winword.exe)的路径。如果安装了这些应用程序,Firefox.exe和Chrome.exe也会存在类似的密钥。

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ App Paths HKEY_CURRENT_USER \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ App Paths

What I want to know is where exactly does the command prompt look for executables?

我想知道的是命令提示符到底在哪里寻找可执行文件?

System PATH Variable, User PATH Variable, and the various keys within ..\App Paths. I was able to confirm that Audacity does not create a key for itself when it's installed.

系统PATH变量,用户PATH变量以及.. \ App路径中的各种键。我能够确认Audacity在安装时没有为自己创建密钥。

When the ShellExecuteEx function is called with the name of an executable file in its lpFile parameter, there are several places where the function looks for the file. We recommend registering your application in the App Paths registry subkey. Doing so avoids the need for applications to modify the system PATH environment variable.

当在其lpFile参数中使用可执行文件的名称调用ShellExecuteEx函数时,该函数会在几个地方查找该文件。我们建议您在App Paths注册表子项中注册您的应用程序。这样做可以避免应用程序修改系统PATH环境变量的需要。

  • The current working directory.
  • 当前的工作目录。
  • The Windows directory only (no subdirectories are searched).
  • 仅限Windows目录(不搜索子目录)。
  • The Windows\System32 directory.
  • Windows \ System32目录。
  • Directories listed in the PATH environment variable.
  • PATH环境变量中列出的目录。
  • Recommended: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
  • 推荐:HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ App Paths

Source: Application Registration

来源:申请注册

#2


8  

From the command prompt, if you just enter WinWord it fails to run.

从命令提示符,如果您只是输入WinWord它无法运行。

If you enter START WinWord it runs.

如果您输入START WinWord,它将运行。

The Start command is key here.

Start命令在这里是关键。

When you try to execute a file through the start command, Command Prompt does not perform any searching. Instead, it passes the file name (and arguments) over to Windows itself (via the ShellExecuteEx API call), which must then search for the file's location. There are several places it searches in the following order:

当您尝试通过start命令执行文件时,命令提示符不会执行任何搜索。相反,它将文件名(和参数)传递给Windows本身(通过ShellExecuteEx API调用),然后必须搜索文件的位置。它按以下顺序搜索了几个地方:

  • The current working directory.

    当前的工作目录。

  • The Windows directory only (no subdirectories are searched).

    仅限Windows目录(不搜索子目录)。

  • The Windows\System32 directory.

    Windows \ System32目录。

  • Directories listed in the PATH environment variable.

    PATH环境变量中列出的目录。

  • Recommended:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

    推荐:HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ App Paths

WinWord is in that registry key. The key is there to keep PATH from getting too long.

WinWord在该注册表项中。关键在于保持PATH不会太长。

#3


6  

The program (when you specify its module name without drive/path in command prompt) in Windows command processor (CMD.EXE) can be started when found:

找到时,可以启动Windows命令处理器(CMD.EXE)中的程序(当您在命令提示符中指定其驱动器/路径的模块名称时):

  • by PATH environment variable (both executable and its hardlink/softlink/shortcut with the same name)

    通过PATH环境变量(可执行文件及其同名的硬链接/软链接/快捷方式)

  • by DOSKEY alias

    通过DOSKEY别名

  • by application path from HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths or HKCU\Software\Microsoft\Windows\CurrentVersion\App Paths (when using start command)

    来自HKLM \ Software \ Microsoft \ Windows \ CurrentVersion \ App Paths或HKCU \ Software \ Microsoft \ Windows \ CurrentVersion \ App Paths的应用程序路径(使用启动命令时)

Using this knowledge (especially the last one) you can create your own aliases convenient for you. For example you can create HKCU\Software\Microsoft\Windows\CurrentVersion\App Paths\au.exe with default value of C:\Program Files (x86)\Audacity\Audacity.exe and start this application simply by typing start au in command prompt.

使用这些知识(特别是最后一个),您可以创建自己的方便的别名。例如,您可以创建HKCU \ Software \ Microsoft \ Windows \ CurrentVersion \ App Paths \ au.exe,默认值为C:\ Program Files(x86)\ Audacity \ Audacity.exe,只需在命令中键入start au即可启动此应用程序提示。

#4


-1  

While the other answers are likely to be the specific reason in your case, there is also another answer to your question which could have been the case for some other applications: in the same place you were looking, but with different file extensions.

虽然其他答案可能是您案例中的具体原因,但您的问题还有另一个答案可能是其他一些应用程序的情况:在您查找的同一个地方,但文件扩展名不同。

You specifically said that you were searching for files with extension exe. Windows will also attempt to execute files of other extensions.

您明确表示您正在搜索扩展名为exe的文件。 Windows还将尝试执行其他扩展的文件。

Another environment variable that comes into play when executing a command is the variable PATHEXT. This is a ;-delimited list of file extensions to attempt to execute. If you echo PATHEXT you might see something like .COM;.EXE;.BAT;.CMD;.VBS;... (etc.). Some applications use these other file types as their end-user entry point. It is much less common, but it happens. I have used several major commercial products which start from .BAT scripts. To use one of them as an example, I can start it with the command standalone even though there is no standalone.exe... instead, it has a standalone.bat.

执行命令时发挥作用的另一个环境变量是变量PATHEXT。这是一个用于尝试执行的文件扩展名的分隔列表。如果您回显PATHEXT,您可能会看到类似.COM; .EXE; .BAT; .CMD; .VBS; ...(等)的内容。某些应用程序使用这些其他文件类型作为其最终用户入口点。它不常见,但它发生了。我使用了几种从.BAT脚本开始的主要商业产品。要使用其中一个作为示例,我可以使用独立命令启动它,即使没有standalone.exe ...相反,它有一个standalone.bat。

Some of the extensions I have on the PATHEXT I'm looking at right now I have never had an application use. Ones that I have run into much more commonly (but obviously not as much as exe) are: .com, .bat, .vbs, .js, .jar. The first two are windows batch script files, and the other three are file types for specific programming languages which are run from scripts or virtual machines instead of from exes (respectively: visual basic, javascript, and java).

我现在在PATHEXT上看到的一些扩展我现在从未使用过应用程序。我遇到的更常见(但显然没有exe那么多)是:.com,.bat,.vbs,.js,.jar。前两个是Windows批处理脚本文件,另外三个是特定编程语言的文件类型,它们是从脚本或虚拟机而不是从exes(分别是:visual basic,javascript和java)运行的。

#1


30  

At first, I thought that cmd only looks for executables in the directories contained in the PATH variable, so I randomly picked an application - winword.exe (Microsoft Word) and tried to launch it from the command line:

起初,我认为cmd只查找PATH变量中包含的目录中的可执行文件,因此我随机选择了一个应用程序 - winword.exe(Microsoft Word)并尝试从命令行启动它:

The reason winword.exe worked is that a registry key exists which defined the path to Microsoft Word (Winword.exe). A similar key exists for Firefox.exe and Chrome.exe if those applications are installed.

winword.exe工作的原因是存在一个注册表项,它定义了Microsoft Word(Winword.exe)的路径。如果安装了这些应用程序,Firefox.exe和Chrome.exe也会存在类似的密钥。

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ App Paths HKEY_CURRENT_USER \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ App Paths

What I want to know is where exactly does the command prompt look for executables?

我想知道的是命令提示符到底在哪里寻找可执行文件?

System PATH Variable, User PATH Variable, and the various keys within ..\App Paths. I was able to confirm that Audacity does not create a key for itself when it's installed.

系统PATH变量,用户PATH变量以及.. \ App路径中的各种键。我能够确认Audacity在安装时没有为自己创建密钥。

When the ShellExecuteEx function is called with the name of an executable file in its lpFile parameter, there are several places where the function looks for the file. We recommend registering your application in the App Paths registry subkey. Doing so avoids the need for applications to modify the system PATH environment variable.

当在其lpFile参数中使用可执行文件的名称调用ShellExecuteEx函数时,该函数会在几个地方查找该文件。我们建议您在App Paths注册表子项中注册您的应用程序。这样做可以避免应用程序修改系统PATH环境变量的需要。

  • The current working directory.
  • 当前的工作目录。
  • The Windows directory only (no subdirectories are searched).
  • 仅限Windows目录(不搜索子目录)。
  • The Windows\System32 directory.
  • Windows \ System32目录。
  • Directories listed in the PATH environment variable.
  • PATH环境变量中列出的目录。
  • Recommended: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
  • 推荐:HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ App Paths

Source: Application Registration

来源:申请注册

#2


8  

From the command prompt, if you just enter WinWord it fails to run.

从命令提示符,如果您只是输入WinWord它无法运行。

If you enter START WinWord it runs.

如果您输入START WinWord,它将运行。

The Start command is key here.

Start命令在这里是关键。

When you try to execute a file through the start command, Command Prompt does not perform any searching. Instead, it passes the file name (and arguments) over to Windows itself (via the ShellExecuteEx API call), which must then search for the file's location. There are several places it searches in the following order:

当您尝试通过start命令执行文件时,命令提示符不会执行任何搜索。相反,它将文件名(和参数)传递给Windows本身(通过ShellExecuteEx API调用),然后必须搜索文件的位置。它按以下顺序搜索了几个地方:

  • The current working directory.

    当前的工作目录。

  • The Windows directory only (no subdirectories are searched).

    仅限Windows目录(不搜索子目录)。

  • The Windows\System32 directory.

    Windows \ System32目录。

  • Directories listed in the PATH environment variable.

    PATH环境变量中列出的目录。

  • Recommended:
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths

    推荐:HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ App Paths

WinWord is in that registry key. The key is there to keep PATH from getting too long.

WinWord在该注册表项中。关键在于保持PATH不会太长。

#3


6  

The program (when you specify its module name without drive/path in command prompt) in Windows command processor (CMD.EXE) can be started when found:

找到时,可以启动Windows命令处理器(CMD.EXE)中的程序(当您在命令提示符中指定其驱动器/路径的模块名称时):

  • by PATH environment variable (both executable and its hardlink/softlink/shortcut with the same name)

    通过PATH环境变量(可执行文件及其同名的硬链接/软链接/快捷方式)

  • by DOSKEY alias

    通过DOSKEY别名

  • by application path from HKLM\Software\Microsoft\Windows\CurrentVersion\App Paths or HKCU\Software\Microsoft\Windows\CurrentVersion\App Paths (when using start command)

    来自HKLM \ Software \ Microsoft \ Windows \ CurrentVersion \ App Paths或HKCU \ Software \ Microsoft \ Windows \ CurrentVersion \ App Paths的应用程序路径(使用启动命令时)

Using this knowledge (especially the last one) you can create your own aliases convenient for you. For example you can create HKCU\Software\Microsoft\Windows\CurrentVersion\App Paths\au.exe with default value of C:\Program Files (x86)\Audacity\Audacity.exe and start this application simply by typing start au in command prompt.

使用这些知识(特别是最后一个),您可以创建自己的方便的别名。例如,您可以创建HKCU \ Software \ Microsoft \ Windows \ CurrentVersion \ App Paths \ au.exe,默认值为C:\ Program Files(x86)\ Audacity \ Audacity.exe,只需在命令中键入start au即可启动此应用程序提示。

#4


-1  

While the other answers are likely to be the specific reason in your case, there is also another answer to your question which could have been the case for some other applications: in the same place you were looking, but with different file extensions.

虽然其他答案可能是您案例中的具体原因,但您的问题还有另一个答案可能是其他一些应用程序的情况:在您查找的同一个地方,但文件扩展名不同。

You specifically said that you were searching for files with extension exe. Windows will also attempt to execute files of other extensions.

您明确表示您正在搜索扩展名为exe的文件。 Windows还将尝试执行其他扩展的文件。

Another environment variable that comes into play when executing a command is the variable PATHEXT. This is a ;-delimited list of file extensions to attempt to execute. If you echo PATHEXT you might see something like .COM;.EXE;.BAT;.CMD;.VBS;... (etc.). Some applications use these other file types as their end-user entry point. It is much less common, but it happens. I have used several major commercial products which start from .BAT scripts. To use one of them as an example, I can start it with the command standalone even though there is no standalone.exe... instead, it has a standalone.bat.

执行命令时发挥作用的另一个环境变量是变量PATHEXT。这是一个用于尝试执行的文件扩展名的分隔列表。如果您回显PATHEXT,您可能会看到类似.COM; .EXE; .BAT; .CMD; .VBS; ...(等)的内容。某些应用程序使用这些其他文件类型作为其最终用户入口点。它不常见,但它发生了。我使用了几种从.BAT脚本开始的主要商业产品。要使用其中一个作为示例,我可以使用独立命令启动它,即使没有standalone.exe ...相反,它有一个standalone.bat。

Some of the extensions I have on the PATHEXT I'm looking at right now I have never had an application use. Ones that I have run into much more commonly (but obviously not as much as exe) are: .com, .bat, .vbs, .js, .jar. The first two are windows batch script files, and the other three are file types for specific programming languages which are run from scripts or virtual machines instead of from exes (respectively: visual basic, javascript, and java).

我现在在PATHEXT上看到的一些扩展我现在从未使用过应用程序。我遇到的更常见(但显然没有exe那么多)是:.com,.bat,.vbs,.js,.jar。前两个是Windows批处理脚本文件,另外三个是特定编程语言的文件类型,它们是从脚本或虚拟机而不是从exes(分别是:visual basic,javascript和java)运行的。