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

查看:253
本文介绍了如何从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).

推荐答案

有关如何执行此操作的文档可以在

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

要以编程方式添加或修改系统环境变量,请将它们添加到 HKEY_LOCAL_MACHINE \ System \ CurrentControlSet \ Control \ Session Manager \ Environment 注册表项中,然后广播

To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment 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_USER \ Environment 中的值.与以前一样,请确保广播

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_USER\Environment. 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天全站免登陆