我可以在 Windows 中使用快捷方式为应用程序设置环境变量吗?
我有一种感觉,我应该能够将一个目录添加到 PATH 环境变量中应用程序生命周期的基础,但我不知道如何做到这一点。是否可以向 Windows 快捷方式添加一个参数,将目录附加到 PATH 的当前值以供所链接的应用程序使用?
I have a feeling I should be able add a directory to the PATH environment 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如这里所解释的: http://www. labs64.com/blog/2012/06/set-environment-variables-in-windows-shortcut/
你也可以在没有bat文件的情况下完成它。
将目标设置为例如:
为了避免在再次关闭之前看到命令提示符,您应该
在“快捷方式”选项卡上
进行设置(在 Windows 7、Windows 10 上测试)
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.
Set Target to e.g.:
To avoid see the command prompt for a split second before it close again, you should set
on the Shortcut tab
(Tested on Windows 7, Windows 10)
让快捷方式执行批处理文件(.cmd),该文件
应该看起来像这样:
Let the shortcut execute a batch file (.cmd), that
Should look like this:
直接链接到批处理文件会产生一个令人讨厌的控制台,您可能希望避免这种情况。这是一个解决方法。更简单的解决方案是使用链接中的“启动最小化”选项,但在 Windows 7 上,您会看到一个瞬时控制台点亮任务栏。
start.bat:
快捷方式目标:
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.
start.bat:
shortcut target:
您可以使用 PowerShell 轻松完成此操作。 PowerShell 使用
$env:
前缀公开环境变量。例如,我想使用自定义JAVA_HOME
和PATH
环境变量启动 TeamSQL,这样我就可以连接到 PostgreSQL 数据库。为此,TeamSQL 依赖 JDK / OpenJDK。首先,我下载了预构建的 OpenJDK 并使用 7-Zip 提取了 ZIP 存档。
接下来,在 PowerShell 中,我运行了以下命令:
将该 PowerShell 代码存储在
.ps1
文件中,您可以使用 PowerShell 运行它。由于子进程从 PowerShell 会话继承环境变量,因此您的程序可以正常运行。You can do this with PowerShell easily. PowerShell exposes environment variables using the
$env:
prefix. For example, I wanted to launch TeamSQL with customJAVA_HOME
andPATH
environment variables, so I could connect to a PostgreSQL database. TeamSQL depends on JDK / OpenJDK for this purpose.First, I downloaded pre-built OpenJDK and extracted the ZIP archive with 7-Zip.
Next, in PowerShell, I ran the following:
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.