我可以在 Windows 中使用快捷方式为应用程序设置环境变量吗?

发布于 2024-09-05 12:48:25 字数 197 浏览 13 评论 0原文

我有一种感觉,我应该能够将一个目录添加到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

清风夜微凉 2024-09-12 12:48:25

正如这里所解释的: http://www. labs64.com/blog/2012/06/set-environment-variables-in-windows-shortcut/
你也可以在没有bat文件的情况下完成它。

将目标设置为例如:

C:\Windows\System32\cmd.exe /c "SET path=%path%&& START /D ^"C:\Program Files (x86)\Notepad++^" notepad++.exe"

为了避免在再次关闭之前看到命令提示符,您应该

Run: Minimized 

在“快捷方式”选项卡上

进行设置(在 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.:

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)

笨笨の傻瓜 2024-09-12 12:48:25

让快捷方式执行批处理文件(.cmd),该文件

  • 设置环境变量
  • 执行应用程序
  • 您使用“START”来执行应用程序,这将在另一个进程中启动应用程序,但它会复制环境。您无需等待应用程序完成。
  • 现在您可以退出批处理文件。

应该看起来像这样:

@echo off
set path=%path%;C:\My Folder
start "Window Title" "Path to my exe"

Let the shortcut execute a batch file (.cmd), that

  • 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.

Should look like this:

@echo off
set path=%path%;C:\My Folder
start "Window Title" "Path to my exe"
寒尘 2024-09-12 12:48:25

直接链接到批处理文件会产生一个令人讨厌的控制台,您可能希望避免这种情况。这是一个解决方法。更简单的解决方案是使用链接中的“启动最小化”选项,但在 Windows 7 上,您会看到一个瞬时控制台点亮任务栏。

start.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

快捷方式目标:

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

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:

@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
眉黛浅 2024-09-12 12:48:25

您可以使用 PowerShell 轻松完成此操作。 PowerShell 使用 $env: 前缀公开环境变量。例如,我想使用自定义 JAVA_HOMEPATH 环境变量启动 TeamSQL,这样我就可以连接到 PostgreSQL 数据库。为此,TeamSQL 依赖 JDK / OpenJDK。

首先,我下载了预构建的 OpenJDK 并使用 7-Zip 提取了 ZIP 存档。

接下来,在 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

将该 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 custom JAVA_HOME and PATH 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:

$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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文