简体   繁体   English

“注册”一个 .exe,以便您可以从 Windows 中的任何命令行运行它

[英]"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,打开开始菜单,

  1. Type Edit environment variables类型Edit environment variables
  2. Open the option Edit the system environment variables打开选项Edit the system environment variables
  3. Click Environment variables... button单击Environment variables...按钮
  4. There you see two boxes, in System Variables box find path variable在那里你看到两个框,在System Variables框中找到path变量
  5. Click Edit单击Edit
  6. a window pops up, click New弹出一个窗口,点击New
  7. Type the Directory path of your .exe or batch file ( Directory means exclude the file name from path)键入.exebatch文件的目录路径(目录意味着从路径中排除文件名)
  8. Click Ok on all open windows and restart your system restart the command prompt .在所有打开的窗口上单击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 等工具用于注册全局命令的方法几乎相同。

Simple Bash-like aliases in Windows Windows 中类似 Bash 的简单别名

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 的别名,而无需手动将每个应用程序添加到路径中,这是我想出的最干净的解决方案,它对系统所做的更改最少,并且拥有最多以后定制的灵活性:

"Install" Your Aliases Path “安装”您的别名路径

mkdir c:\aliases
setx PATH "c:\aliases;%PATH%"

Add Your Alias添加您的别名

Open in New Shell Window在新外壳窗口中打开

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" %*

Execute in Current Shell Window在当前 Shell 窗口中执行

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

Execute in Current Shell Window 2在当前 Shell 窗口中执行 2

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):

  1. Type in search bar "environment..." and choose Edit the system environment variables which opens up the System Properties window在搜索栏中输入“环境...”并选择编辑系统环境变量,打开系统属性窗口
  2. Click the Environment Variables... button单击环境变量... 按钮
  3. In the Environment Variables tab, double click the Path variable in the System variables section在环境变量选项卡中,双击系统变量部分中的路径变量
  4. Add the path to the folder containing the .exe to the Path by double clicking on the empty line and paste the path.通过双击空行并粘贴路径,将包含 .exe 的文件夹的路径添加到 Path。
  5. Click ok and exit.单击确定并退出。 Open a new cmd prompt and hit the command from any folder and it should work.打开一个新的 cmd 提示符并从任何文件夹中点击该命令,它应该可以工作。

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参考: 更改系统/用户变量

  • If you want to be able to run it inside cmd.exe or batch files you need to add the directory the .exe is in to the %path% variable ( System or User )如果您希望能够在 cmd.exe 或批处理文件中运行它,您需要将 .exe 所在的目录添加到 %path% 变量( System 或 User
  • If you want to be able to run it in the Run dialog (Win+R) or any application that calls ShellExecute , adding your exe to the app paths key is enough (This is less error prone during install/uninstall and also does not clutter up the path variable)如果您希望能够在“运行”对话框 (Win+R) 或任何调用ShellExecute的应用程序中运行它,将您的 exe 添加到应用程序路径键就足够了(这在安装/卸载过程中不易出错,也不会混乱向上路径变量)

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:这对我有用:

  • put a .bat file with the commands you need (I use to run .py script into this) into a FOLDER,将带有所需命令的 .bat 文件(我用来在其中运行 .py 脚本)放入文件夹中,
  • go in the variable environment setting (type var in the search bar and it will show up)进入变量环境设置(在搜索栏中输入 var 它会显示出来)
  • in the global settings在全局设置中
    • choose path,选择路径,
    • then modify,然后修改,
    • then add the path to your .bat file (without the .bat file)然后将路径添加到您的 .bat 文件(没有 .bat 文件)
    • close everything: done.关闭一切:完成。

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

  1. 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/”

  2. go in enviromental variable settings from the search bar in the bottom left of the window desktop从窗口桌面左下角的搜索栏中进入环境变量设置

  3. go in enviromental variables (bottom button) then in path (bottom)进入环境变量(底部按钮)然后进入路径(底部)

  4. add the path, for example G:\myapp_launcher添加路径,例如 G:\myapp_launcher

  5. 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:我不是程序员或类似的人,但这是我的简单解决方案:

  • Create a folder in which you'll be putting SHORTCUTS for all the programs you want to register;创建一个文件夹,您将在其中放置您要注册的所有程序的快捷方式;
  • Add that folder to the PATH ;将该文件夹添加到PATH
  • Put all the shortcuts you want in the folder you created in the first step (context menu, New, Shortcut...) The SHORTCUT NAME will have be the be summoned when calling the program or function... NOT THE TARGET FILE NAME .将您想要的所有快捷方式放在您在第一步中创建的文件夹中(上下文菜单、新建、快捷方式...)调用程序或函数时将调用快捷方式名称...而不是目标文件名

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/142882https://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.

相关问题 Windows:如果不从命令行运行,是否可以访问运行C ++代码的进程的返回值? - Windows: Can the return value of a process running C++ code be accessed if you don't run from command line? 如何从Windows命令行运行没有标题栏的exe文件? - How to run an exe file from windows command line without the title bar? 如何从命令行(Windows 7)编译和运行项目? - How can I compile and run project from command line(Windows 7)? 有什么方法可以重定向Windows命令行中以“开始”运行的命令的stderr输出? - Is there any way to redirect stderr output from a command run with “start” in the Windows command line? 有什么方法可以重定向 Windows 命令行中使用“start”运行的命令的 stderr 输出? - Is there any way to redirect stderr output from a command run with "start" in the Windows command line? 无法通过Ant运行Windows命令,但可以在cmd.exe上运行 - Cannot run a Windows command via Ant, but can on cmd.exe 从命令行窗口运行python无法正常工作 - run python from command line windows not working 如何在 Windows 上从命令行运行 clang? - How run clang from command line on Windows? 使用命令行参数从bat文件运行exe - Run exe from bat file with command line arguments 从不是子进程的命令行在Windows中启动EXE - Start an EXE in Windows from the Command Line that is not a Child Process
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM