我可以在 Windows 中使用快捷方式为应用程序设置环境变量吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3036325/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I set an environment variable for an application using a shortcut in Windows?
提问by user366203
I have a feeling I should be able add a directory to the PATHenvironment variable on an application-lifetime basis, but I can't find out how to do this. Is it possible to add a parameter to a Windows shortcut that appends a directory to the current value of PATH for use by the application being linked?
我有一种感觉,我应该能够在应用程序生命周期的基础上向PATH环境变量添加一个目录,但我不知道如何做到这一点。是否可以向 Windows 快捷方式添加参数,该快捷方式将目录附加到 PATH 的当前值以供被链接的应用程序使用?
采纳答案by GvS
Let the shortcut execute a batch file (.cmd), that
让快捷方式执行一个批处理文件 (.cmd),即
- Sets the environment variable
- execute the app
- You use "START" to execute the app, this will start the app in another process, but it will copy the environment. You do not wait for the app to finish.
- Now you can exit the batch file.
- 设置环境变量
- 执行应用程序
- 您使用“START”来执行应用程序,这将在另一个进程中启动应用程序,但它会复制环境。您无需等待应用程序完成。
- 现在您可以退出批处理文件。
Should look like this:
应该是这样的:
@echo off
set path=%path%;C:\My Folder
start "Path to my exe"
回答by Jens
As explained here: http://www.labs64.com/blog/2012/06/set-environment-variables-in-windows-shortcut/you can do it without a bat file too.
如此处所述:http: //www.labs64.com/blog/2012/06/set-environment-variables-in-windows-shortcut/您也可以在没有 bat 文件的情况下进行。
Set Target to e.g.:
将目标设置为例如:
C:\Windows\System32\cmd.exe /c "SET path=%path%&& START /D ^"C:\Program Files (x86)\Notepad++^" notepad++.exe"
To avoid see the command prompt for a split second before it close again, you should set
为避免在再次关闭之前看到命令提示符一瞬间,您应该设置
Run: Minimized
on the Shortcut tab
在快捷方式选项卡上
(Tested on Windows 7, Windows 10)
(在 Windows 7、Windows 10 上测试)
回答by Will Bickford
Linking directly to a batch file spawns an annoying console that you probably want to avoid. Here's a work-around. The simpler solution is to use the "Start Minimized" option in your link, but on Windows 7 you'll see a momentary console light up your task bar.
直接链接到批处理文件会产生一个您可能想要避免的烦人的控制台。这是一个解决方法。更简单的解决方案是使用链接中的“开始最小化”选项,但在 Windows 7 上,您会看到一个瞬时控制台点亮您的任务栏。
start.bat:
开始.bat:
@echo off
IF "%1" == "" GOTO Error
IF "%2" == "" GOTO Error
IF NOT EXIST %2 GOTO Error
SET PATH=%1;%PATH%
start %2
GOTO End
:Error
echo Problem!
pause
:End
shortcut target:
快捷目标:
MyPath = "C:\MyApp"
Set shell = WScript.CreateObject("WScript.Shell")
cmd = "start.bat " & MyPath & " MyApp.exe"
shell.Run cmd, 0, false
Set env = Nothing
Set shell = Nothing
回答by Trevor Sullivan
You can do this with PowerShell easily. PowerShell exposes environment variables using the $env:
prefix. For example, I wanted to launch TeamSQL with custom JAVA_HOME
and PATH
environment variables, so I could connect to a PostgreSQL database. TeamSQL depends on JDK / OpenJDK for this purpose.
您可以使用 PowerShell 轻松完成此操作。PowerShell 使用$env:
前缀公开环境变量。例如,我想使用自定义变量JAVA_HOME
和PATH
环境变量启动 TeamSQL ,这样我就可以连接到 PostgreSQL 数据库。为此,TeamSQL 依赖于 JDK / OpenJDK。
First, I downloaded pre-built OpenJDK and extracted the ZIP archive with 7-Zip.
首先,我下载了预先构建的 OpenJDK 并使用 7-Zip 解压缩了 ZIP 存档。
Next, in PowerShell, I ran the following:
接下来,在 PowerShell 中,我运行了以下命令:
$env:JAVA_HOME='C:\Users\TrevorSullivan\Downloads\openjdk\jdk-11.0.2\'
$env:PATH += ';%JAVA_HOME%\bin'
# Launch TeamSQL
& C:\Users\TrevorSullivan\AppData\Local\Programs\TeamSQL\TeamSQL.exe
Store that PowerShell code in a .ps1
file, and you can run it with PowerShell. Because child processes inherit the environment variables from the PowerShell session, your program is good to go.
将该 PowerShell 代码存储在一个.ps1
文件中,您可以使用 PowerShell 运行它。由于子进程从 PowerShell 会话继承环境变量,因此您的程序可以正常运行。