如何从 Windows 命令行永久更新 PATH 变量? [英] How to update PATH variable permanently from Windows command line?

查看:60
本文介绍了如何从 Windows 命令行永久更新 PATH 变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我从命令行 (cmd.exe) 执行 set PATH=%PATH%;C:\Something\bin 然后执行 echo %PATH% 我看到这个字符串被添加到了 PATH 中.如果我关闭并打开命令行,则该新字符串不在 PATH 中.

If I execute set PATH=%PATH%;C:\Something\bin from the command line (cmd.exe) and then execute echo %PATH% I see this string added to the PATH. If I close and open the command line, that new string is not in PATH.

如何从命令行为将来的所有进程永久更新 PATH,而不仅仅是当前进程?

How can I update PATH permanently from the command line for all processes in the future, not just for the current process?

我不想通过转到系统属性"→高级"→环境变量"并在那里更新 PATH 来执行此操作.

I don't want to do this by going to System Properties → Advanced → Environment variables and update PATH there.

此命令必须从 Java 应用程序执行(请参阅我的其他 问题).

This command must be executed from a Java application (please see my other question).

推荐答案

有关如何执行此操作的文档可以在 MSDN.关键摘录是这样的:

The documentation on how to do this can be found on MSDN. The key extract is this:

要以编程方式添加或修改系统环境变量,请将它们添加到 HKEY_LOCAL_MACHINESystemCurrentControlSetControlSession ManagerEnvironment 注册表项,然后广播 WM_SETTINGCHANGE 带有 lParam 设置的消息到字符串环境".这允许应用程序(例如 shell)获取您的更新.

To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINESystemCurrentControlSetControlSession ManagerEnvironment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates.

请注意,您的应用程序需要提升管理员权限才能修改此密钥.

Note that your application will need elevated admin rights in order to be able to modify this key.

您在评论中表示您很乐意只修改每个用户的环境.为此,请编辑 HKEY_CURRENT_USEREnvironment 中的值.和以前一样,确保您广播 WM_SETTINGCHANGE 消息.

You indicate in the comments that you would be happy to modify just the per-user environment. Do this by editing the values in HKEY_CURRENT_USEREnvironment. As before, make sure that you broadcast a WM_SETTINGCHANGE message.

您应该能够使用 JNI 注册表类轻松地从 Java 应用程序执行此操作.

You should be able to do this from your Java application easily enough using the JNI registry classes.

这篇关于如何从 Windows 命令行永久更新 PATH 变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆