[英]"Register" an .exe so you can run it from any command line in Windows
How can you make a .exe file accessible from any location in the Windows command window?如何使 .exe 文件可以从 Windows 命令窗口中的任何位置访问? Is there some registry entry that has to be entered?
是否有一些必须输入的注册表项?
You need to make sure that the exe is in a folder that's on the PATH
environment variable.您需要确保 exe 位于
PATH
环境变量中的文件夹中。
You can do this by either installing it into a folder that's already on the PATH
or by adding your folder to the PATH
.您可以通过将其安装到已经在
PATH
上的文件夹中或将您的文件夹添加到PATH
中来做到这一点。
You can have your installer do this - but you may need to restart the machine to make sure it gets picked up.您可以让您的安装程序执行此操作 - 但您可能需要重新启动机器以确保它被拾取。
Windows 10, 8.1, 8视窗 10、8.1、8
Open start menu,打开开始菜单,
Edit environment variables
Edit environment variables
Edit the system environment variables
Edit the system environment variables
Environment variables...
buttonEnvironment variables...
按钮System Variables
box find path
variableSystem Variables
框中找到path
变量Edit
Edit
New
New
.exe
or batch
file ( Directory means exclude the file name from path).exe
或batch
文件的目录路径(目录意味着从路径中排除文件名)Ok
on all open windows and
Ok
并
You can add the following registry key :您可以添加以下注册表项:
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\myexe.exe
In this key, add the default string value containing the path to the exe file.在此项中,添加包含 exe 文件路径的默认字符串值。
You have to put your .exe
file's path into enviroment variable path.您必须将
.exe
文件的路径放入环境变量路径中。 Go to "My computer -> properties -> advanced -> environment variables -> Path" and edit path by adding .exe
's directory into path.转到“我的电脑 -> 属性 -> 高级 -> 环境变量 -> 路径”并通过将
.exe
的目录添加到路径中来编辑路径。
Another solution I personally prefer is using RapidEE for a smoother variable editing.我个人更喜欢的另一个解决方案是使用RapidEE进行更流畅的变量编辑。
Rather than putting the executable into a directory on the path, you should create a batch file in a directory on the path that launches the program.与其将可执行文件放在路径上的目录中,不如在启动程序的路径上的目录中创建一个批处理文件。 This way you don't separate the executable from its supporting files, and you don't add other stuff in the same directory to the path unintentionally.
这样您就不会将可执行文件与其支持文件分开,也不会无意中将同一目录中的其他内容添加到路径中。
Such batch file can look like this:这样的批处理文件可能如下所示:
@echo off
start "" "C:\Program Files (x86)\Software\software.exe" %*
Let's say my exe is C:\Program Files\AzCopy\azcopy.exe假设我的 exe 是 C:\Program Files\AzCopy\azcopy.exe
Command/CMD/Batch命令/CMD/批处理
SET "PATH=C:\Program Files\AzCopy;%PATH%"
PowerShell电源外壳
$env:path = $env:path + ";C:\Program Files\AzCopy"
I can now simply type and use azcopy
from any location from any shell inc command prompt, powershell, git bash etc我现在可以从任何 shell inc 命令提示符、powershell、git bash 等的任何位置简单地键入和使用
azcopy
It is very simple and it won't take more than 30 seconds.它非常简单,不会超过 30 秒。
For example the software called abc located in D:/Softwares/vlc/abc.exe Add the folder path of abc.exe to system environment variables.例如位于 D:/Softwares/vlc/abc.exe 的名为 abc 的软件,将 abc.exe 的文件夹路径添加到系统环境变量中。
My Computer -> Click Properties -> Click Advanced system settings -> Click Environment Variables我的电脑 -> 点击属性 -> 点击高级系统设置 -> 点击环境变量
Click on Ok.单击确定。
now you can just open cmd prompt and you can launch the software from anywhere.现在您只需打开 cmd 提示符即可从任何地方启动该软件。 to use abc.exe just type abc in the command line.
要使用 abc.exe,只需在命令行中键入 abc。
it's amazing there's no simple solution for such a simple task on windows, I created this little cmd script that you can use to define aliases on windows (instructions are at the file header itself):令人惊讶的是,在 windows 上这样一个简单的任务没有简单的解决方案,我创建了这个小 cmd 脚本,您可以使用它来定义 windows 上的别名(说明位于文件头本身):
https://gist.github.com/benjamine/5992592 https://gist.github.com/benjamine/5992592
this is pretty much the same approach used by tools like NPM or ruby gems to register global commands.这与 NPM 或 ruby gems 等工具用于注册全局命令的方法几乎相同。
To get global bash-like aliases in Windows for applications not added to the path automatically without manually adding each one to the path, here's the cleanest solution I've come up with that does the least amount of changes to the system and has the most flexibility for later customization:要在 Windows 中为未自动添加到路径的应用程序获取类似 bash 的别名,而无需手动将每个应用程序添加到路径中,这是我想出的最干净的解决方案,它对系统所做的更改最少,并且拥有最多以后定制的灵活性:
mkdir c:\aliases
setx PATH "c:\aliases;%PATH%"
To start C:\path to\my program.exe
, passing in all arguments, opening it in a new window, create c:\aliases\my program.bat
file with the following contents(see NT Start Command for details on the start commmand):要启动
C:\path to\my program.exe
,传入所有参数,在新窗口中打开它,创建c:\aliases\my program.bat
文件,内容如下(有关启动的详细信息,请参见NT 启动命令命令):
@echo off
start "myprogram" /D "C:\path to\" /W "myprogram.exe" %*
To start C:\path to\my program.exe
, passing in all arguments, but running it in the same window (more like how bash operates) create c:\aliases\my program.bat
file with the following contents:要启动
C:\path to\my program.exe
,传入所有参数,但在同一窗口中运行它(更像 bash 的操作方式),请创建c:\aliases\my program.bat
文件,其中包含以下内容:
@echo off
pushd "C:\path to\"
"my program.exe" %*
popd
If you don't need the application to change the current working directory at all in order to operate, you can just add a symlink to the executable inside your aliases folder:如果您根本不需要应用程序更改当前工作目录即可运行,您只需将符号链接添加到您的别名文件夹中的可执行文件:
cd c:\aliases\
mklink "my program.exe" "c:\path to\my program.exe"
Add to the PATH, steps below (Windows 10):添加到 PATH,步骤如下(Windows 10):
You may also permanently (after reboots) add to the Path variable this way:您也可以通过这种方式永久(重新启动后)添加到Path变量中:
Right click My Computer -> Click Properties -> Click Advanced system settings -> Click Environment Variables右键我的电脑->点击属性->点击高级系统设置->点击环境变量
Reference: Change System/User Variables参考: 更改系统/用户变量
Put it in the c:\windows directory or add your directory to the "path" in the environment-settings (windows-break - tab advanced)将其放在 c:\windows 目录中或将您的目录添加到环境设置中的“路径”(windows-break - tab advanced)
regards, //t问候,//t
In order to make it work为了使它工作
You need to modify the value of the environment variable with the name key Path
, you can add as many paths as you want separating them with ;
您需要使用名称键
Path
修改环境变量的值,您可以添加任意数量的路径,用;
分隔它们。 . . The paths you give to it can't include the name of the executable file.
您给它的路径不能包含可执行文件的名称。
If you add a path to the variable Path
all the excecutable files inside it can be called from cmd or porweshell by writing their name without .exe
and these names are not case sensitive.如果为变量
Path
添加路径,则可以从 cmd 或 porweshell 调用其中的所有可执行文件,方法是在不带.exe
的情况下写入它们的名称,并且这些名称不区分大小写。
Here is how to create a system environment variable from a python script:以下是如何从 python 脚本创建系统环境变量:
It is important to run it with administrator privileges in order to make it work.以管理员权限运行它以使其工作非常重要。 To better understand the code, just read the comments on it.
为了更好地理解代码,只需阅读上面的注释。
Tested on Windows 10在 Windows 10 上测试
import winreg
# Create environment variable for call the program from shell, only works with compiled version
def environment_var(AppPath):
# Point to the registry key of the system environment variables
key = winreg.CreateKey(winreg.HKEY_LOCAL_MACHINE, r'System\CurrentControlSet\Control\Session Manager\Environment')
def add_var(path):
# Add the variable
winreg.SetValueEx(key, 'Path', 0, winreg.REG_SZ, path)
winreg.CloseKey(key)
try:
# Try to get the value of the Path variable
allPaths = winreg.QueryValueEx(key, 'Path')[0]
except Exception:
# Create the Path variable if it doesn't exist
add_var(path=AppPath)
return
# Get all the values of the existing paths
Path=allPaths.split(';')
# If the Path is empty, add the application path
if Path == ['']:
add_var(path=AppPath)
return
# Check if the application path is in the Path variable
if AppPath not in Path:
# Add the application path to the Path environment variable and add keep the others existing paths
add_var(path=AppPath+';'+allPaths)
# Only run this if the module is not imported by another
if __name__ == "__main__":
# Run the function
environment_var(AppPath=".")
You can find more information in the winreg documentation您可以在winreg 文档中找到更多信息
This worked for me:这对我有用:
Open the cmd, write the name of the .bat file and it will work打开cmd,写下.bat文件的名字就可以了
Example例子
Want to open chrome on a specific link想要在特定链接上打开 chrome
create a .bat file with this (save it as blog.bat for example)用这个创建一个 .bat 文件(例如,将其保存为 blog.bat )
start "" "https://pythonprogramming.altervista.org/"开始“”“https://pythonprogramming.altervista.org/”
go in enviromental variable settings from the search bar in the bottom left of the window desktop从窗口桌面左下角的搜索栏中进入环境变量设置
go in enviromental variables (bottom button) then in path (bottom)进入环境变量(底部按钮)然后进入路径(底部)
add the path, for example G:\myapp_launcher添加路径,例如 G:\myapp_launcher
click apply or ok单击应用或确定
Now open cmd and write blog: chrome will open on that page现在打开 cmd 并写博客:chrome 将在该页面上打开
Do the same to open a file... create a .bat in the folder G:\myapp_launcher (or whatever you called the folder where you put the batch file), call it run.bat or myapp.bat or whatever (write inside of it start filemane.pdf or whatever file you want to open) and after you saved it, you can run that file from cmd with run or myapp or whatever you called your batch file.做同样的事情来打开一个文件...在文件夹 G:\myapp_launcher (或任何你称之为放置批处理文件的文件夹)中创建一个 .bat,称之为 run.bat 或 myapp.bat 或其他任何东西(写在里面其中启动 filemane.pdf 或您要打开的任何文件),保存后,您可以使用 run 或 myapp 或任何您称为批处理文件的文件从 cmd 运行该文件。
You can also move your files to C:\Windows
, but you need to use Administrator privileges and pay attention .您也可以将文件移动到
C:\Windows
,但您需要使用管理员权限并注意。
What did I mean with pay attention?我说的注意是什么意思?
You need pay attention because you can also do some messes with Windows system files (Windows may not even work anymore) if you modify, delete, and do some changes incorrectly and accidentally in this folder...您需要注意,因为如果您在此文件夹中不正确地和意外地修改、删除和进行一些更改,您也可能会弄乱 Windows 系统文件(Windows 甚至可能不再工作)......
Example: Don't add a file that have the same name of a Windows file示例:不要添加与 Windows 文件同名的文件
Use a 1 line batch file in your install:在安装中使用 1 行批处理文件:
SETX PATH "C:\Windows"
run the bat file运行bat文件
Now place your .exe in c:\windows, and you're done.现在将你的.exe 文件放在c:\windows 中,你就完成了。
you may type the 'exename' in command-line and it'll run it.您可以在命令行中键入“exename”,它会运行它。
Another way could be through adding .LNK
to your $PATHEX.另一种方法是通过将
.LNK
添加到您的 $PATHEX。 Then just create a shortcut to your executable (ie: yourshortcut.lnk) and put it into any of the directories listed within $PATH.然后只需为您的可执行文件创建一个快捷方式(即:yourshortcut.lnk)并将其放入$PATH 中列出的任何目录中。
WARNING NOTE: Know that any .lnk files located in any directories listed in your $PATH are now "PATH'ed" as well.警告注意:知道位于 $PATH 中列出的任何目录中的任何 .lnk 文件现在也都是“PATH'ed”。 For this reason, I would favor the batch file method mentionned earlier to this method.
出于这个原因,我更喜欢前面提到的批处理文件方法。
I'm not a programmer or anything of the sort, but here's my simple solution:我不是程序员或类似的人,但这是我的简单解决方案:
PATH
;PATH
; This will keep you from unintentionally putting files you don't want in the PATH
.这将防止您无意中将不需要的文件放入
PATH
中。
Feel free to drop a comment if you think this answer needs to be improved.如果您认为此答案需要改进,请随时发表评论。 Cheers 🍻.
干杯🍻。
PS No system or File Explorer restart needed. PS 无需重新启动系统或文件资源管理器。 😀
😀
最好的方法是将 .EXE 文件的文件夹路径添加到环境变量中的路径值中。
I'm not sure what versions of Windows this works with, but I put some useful .bat
and .exe
files into:我不确定这适用于哪些版本的 Windows,但我将一些有用的
.bat
和.exe
文件放入:
%LOCALAPPDATA%\Microsoft\WindowsApps
(equivalent to %USERPROFILE%\AppData\Local\Microsoft\WindowsApps
) (相当于
%USERPROFILE%\AppData\Local\Microsoft\WindowsApps
)
which seems to be on my default PATH.这似乎在我的默认路径上。 I'd be interested to see if this were the general case.
我很想看看这是否是一般情况。
DOSKEY is a Microsoft version of 'alias'. DOSKEY是 Microsoft 版本的“别名”。 That function is already built into all versions of Windows (and most versions of DOS)
该功能已内置于所有版本的 Windows(以及大多数版本的 DOS)中
doskey fred=c:\myApps\myprog.exe
You'll want to load that every time you open a command prompt.每次打开命令提示符时都需要加载它。 Which you can do by any number of different methods.
您可以通过多种不同的方法来做到这一点。 One way is to
一种方法是
Make a file containing all the doskey macros you want:
doskey fred=c:\whatever.exe
doskey alan=c:\whateverelse.exe
Change the file type / file name / file extension to .CMD or .BAT
ren myfile.txt myfile.CMD
Add the CMD/BAT file to your command processor autoruns key:
reg ADD \\HKCU\Software\Microsoft\Command Processor /v autorun /t REG_SZ /d myfile.CMD
For more information see有关更多信息,请参阅
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/doskey https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/doskey
and https://serverfault.com/a/1049766/142882和https://serverfault.com/a/1049766/142882
(serverfault.com/questions/95404/is-there-a-global-persistent-cmd-history)
Should anyone be looking for this after me here's a really easy way to add your Path.如果有人在我之后寻找这个,这是一种添加路径的非常简单的方法。
Send the path to a file like the image shows, copy and paste it from the file and add the specific path on the end with a preceding semicolon to the new path.将路径发送到如图所示的文件,从文件中复制并粘贴它,并在末尾添加特定路径,并在新路径前加上分号。 It may be needed to be adapted prior to windows 7, but at least it is an easy starting point.
它可能需要在 Windows 7 之前进行调整,但至少它是一个简单的起点。
Command Prompt Image to Export PATH to text file将 PATH 导出到文本文件的命令提示符图像
The best way to do this is just install the .EXE file into the windows/system32 folder.最好的方法是将 .EXE 文件安装到 windows/system32 文件夹中。 that way you can run it from any location.
这样你就可以从任何位置运行它。 This is the same place where .exe's like ping can be found
这是可以找到 .exe 之类 ping 的同一个地方
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.