[英]Use Ruby to permanently (ie, in the registry) set environment variables?
On Windows, how can I use Ruby to permanently set an environment variable? 在Windows上,如何使用Ruby永久设置环境变量? I know I need to change the registry (through the win32ole module?) but I am a novice with regard to scripting the registry.
我知道我需要更改注册表(通过win32ole模块?)但我是一个关于脚本注册表的新手。
I understand that I can say ENV['FOO'] = "c:\\bar\\baz"
to set the environment variable FOO
for the session. 据我所知,我可以说
ENV['FOO'] = "c:\\bar\\baz"
为会话设置环境变量FOO
。 However, I am instead interested in setting environment variables globally and permanently. 但是,我对全局和永久设置环境变量感兴趣。
I did find the patheditor gem, which works great for permanently altering the Windows PATH
. 我找到了patheditor gem,它非常适合永久改变Windows
PATH
。 But I want to set other environment variables, for example, JAVA_HOME
. 但是我想设置其他环境变量,例如
JAVA_HOME
。
There is a past question about this. 有一个过去的问题 。 The basic gist is to set the variable in the registry via Win32::Registry (like runako said).
基本要点是通过Win32 :: Registry在注册表中设置变量(如runako所说)。 Then you can broadcast a WM_SETTINGCHANGE message to make changes to the environment.
然后,您可以广播WM_SETTINGCHANGE消息以更改环境。 Of course you could logoff/logon in between then too, but not very usable.
当然你也可以在两者之间注销/登录,但不是很有用。
Registry code: 注册码:
require 'win32/registry.rb'
Win32::Registry::HKEY_CURRENT_USER.open('Environment', Win32::Registry::KEY_WRITE) do |reg|
reg['ABC'] = '123'
end
WM_SETTINGCHANGE code: WM_SETTINGCHANGE代码:
require 'Win32API'
SendMessageTimeout = Win32API.new('user32', 'SendMessageTimeout', 'LLLPLLP', 'L')
HWND_BROADCAST = 0xffff
WM_SETTINGCHANGE = 0x001A
SMTO_ABORTIFHUNG = 2
result = 0
SendMessageTimeout.call(HWND_BROADCAST, WM_SETTINGCHANGE, 0, 'Environment', SMTO_ABORTIFHUNG, 5000, result)
Thanks to Alexander Prokofyev for the answer . 感谢Alexander Prokofyev 的回答 。
Also see a good discussion on Windows environment variables in general, including how to set them for the entire machine vs. just the current user ( in HKEY_LOCAL_MACHINE\\ SYSTEM\\ CurrentControlSet\\ Control\\ Session Manager\\ Environment) 另外,请参阅有关Windows环境变量的一般讨论 ,包括如何为整个计算机设置它们而不仅仅是当前用户(在HKEY_LOCAL_MACHINE \\ SYSTEM \\ CurrentControlSet \\ Control \\ Session Manager \\ Environment中)
You're looking for Win32::Registry : 您正在寻找Win32 :: Registry:
http://www.ruby-doc.org/stdlib/libdoc/Win32API/rdoc/classes/Win32/Registry.html http://www.ruby-doc.org/stdlib/libdoc/Win32API/rdoc/classes/Win32/Registry.html
For reference, here's how I found it: 作为参考,这是我如何找到它:
http://www.google.com/search?client=safari&rls=en-us&q=ruby+registry&ie=UTF-8&oe=UTF-8 http://www.google.com/search?client=safari&rls=en-us&q=ruby+registry&ie=UTF-8&oe=UTF-8
Anyhow, then you will want to do something like: 无论如何,那么你会想做类似的事情:
registry.open("HKEY_WINDOWS_GUNK/path/to/your/key", Win32::Registry::KEY_WRITE) do |reg|
reg[regentry, Win32::Registry::REG_DWORD]=value
end
You might have to create a key first, if it doesn't already exist. 如果密钥尚不存在,则可能必须先创建密钥。
I am pleased to see such a comprehensive set of answers! 我很高兴看到这么全面的答案!
It should also be noted that when creating/writing to entries under reserved/system keys (such as HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node on 64-bit Windows operating system) using constant flags such as Win32::Registry::KEY_WRITE and Win32::Registry::KEY_ALL_ACCESS will not exhibit desired behaviour unless the MRI (the Ruby interpreter) instance is started from an "Administrator" kernel context. 还应注意,在使用常量标志(如Win32 :: Registry :: KEY_WRITE和Win32 :: Registry)创建/写入保留/系统键下的条目(例如64位Windows操作系统上的HKEY_LOCAL_MACHINE \\ SOFTWARE \\ Wow6432Node)时:: KEY_ALL_ACCESS将不会显示所需的行为,除非MRI(Ruby解释器)实例是从“管理员”内核上下文启动的。 Starting cmd.exe (Windows shell program) by right-clicking the executable and selecting "Run as Administrator" allows this.
通过右键单击可执行文件并选择“以管理员身份运行”来启动cmd.exe(Windows shell程序)允许此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.