[译]设置环境
By robot-v1.0
本文链接 https://www.kyfws.com/applications/setenv-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 10 分钟阅读 - 4568 个词 阅读量 0[译]设置环境
原文地址:https://www.codeproject.com/Articles/12153/SetEnv
原文作者:Jonathan [Darka]
译文由本站 robot-v1.0 翻译
前言
A free tool for setting/updating/deleting System Environment Variables.
用于设置/更新/删除系统环境变量的免费工具.
介绍(Introduction)
我最近使用了出色的Inno Setup实用程序为自己的应用程序创建了一些安装脚本.我发现除非使用外部应用程序,否则无法设置系统范围的环境变量.(I recently used the excellent Inno Setup utility to create some install scripts for my own applications. I discovered that there was no way to set a system-wide environment variable except by using an external application.) SetEnv
支持用户和系统环境变量.(supports User and System environment variables.)
我从搜索Microsoft setex工具开始,但找不到它.我并没有真正努力.我以为我会自己写,因此写这篇文章.(I began by searching for the Microsoft setex tool, but couldn’t find it. I didn’t really try that hard though. I thought I’d write my own, hence this article.)
您可以找到适合的设置工具包(You can find a proper setup kit for) SetEnv
在我的网站上.(on my website.)
系统环境变量(System Environment Variables)
用于创建系统范围环境的方法取决于所使用的操作系统.(The method used to create a system-wide environment is dependent upon the operating system in use.) SetEnv
会自动检测到这一点,并将使用以下技术之一来创建/修改或删除环境变量.(will automatically detect this and will use one of the following techniques to create/modify or delete environment variables.)
Windows 95/98/ME(Windows 95/98/ME)
在Windows 9x下,创建环境变量需要修改用户的(Under Windows 9x, creating an environment variable requires modifying the user’s)**autoexec.bat(autoexec.bat)**文件,然后执行该文件或重新启动,然后操作系统会识别该变量.(file and then executing it or rebooting before the variable is recognized by the operating system.) SetEnv
会自动找到(will automatically locate the)**autoexec.bat(autoexec.bat)**文件本身-只要文件位于(file itself – as long as the file is on the)**C:(*C:*)**要么(or)**D:(*D:*)**驱动器-然后添加或修改所选的系统变量.一件事(drive – and then add or modify the selected system variable. The one thing) SetEnv
重新启动PC并不会这样做,因为应由用户或设置工具包(例如,从Inno Setup)决定是否重新启动PC.(will not do is reboot the PC, as it should be left up to the user or the Setup Kit – from Inno Setup, for example – to choose when to perform the reboot.)
Windows XP/2000/2K3/Vista(Windows XP/2000/2K3/Vista)
在Windows 2000,XP和Windows 2003 Server等更现代(即适当)的操作系统下,环境变量存储在注册表中的以下项下:(Under more modern (i.e. proper) operating systems such as Windows 2000, XP, and Windows 2003 Server, environment variables are stored in the registry under the following key:)
HKEY_LOCAL_MACHINE\System\CurrentControlSet\
Control\Session Manager\Environment
通过在此键下创建新值或修改值(如果已存在)来添加变量.要删除变量,我们只需删除其注册表值.也就是说,除非我们要删除一部分扩展值,例如(Variables are added by creating a new value under this key or by modifying a value if it already exists. To delete a variable, we simply delete its registry value. That is, unless we are removing part of an expanded value such as) PATH
,在这种情况下,我们只删除想要的部分.(, in which case we only remove the part we want.)
此时,除非我们注销或重新启动,否则Windows将不会知道我们所做的更改.为了解决这个问题,(At this point, Windows will not be aware of our changes unless we log off or reboot. To get around this,) SetEnv
将播放一个(will broadcast a) WM_SETTINGCHANGE
到系统中的所有Windows.这允许其他正在运行的应用程序,例如(to all of the Windows in the system. This allows other running applications such as)**资源管理器(Explorer.exe)**通知我们的更改.如果你跑(to be notified of our change. If you run) SetEnv
从命令提示符,然后将不会更新当前DOS窗口的环境变量.这主要是由于以下事实:(from a command prompt, then this will not update the environment variable for the current DOS window. This is mainly due to the fact that a process () SetEnv
)无法更改其父级的环境,请在命令提示符下进行.但是,您打开的任何新的DOS/命令提示符都将显示新的变量/值.() cannot change the environment of its parent, the command prompt. However, any new DOS/command prompt that you open will show the new variable/value.)
广播此消息会导致大约2-3秒的轻微延迟,而打开的Windows会处理该消息.所以看起来(Broadcasting this message results in a slight delay of around 2-3 seconds, whilst the open Windows process it. So it may appear that) SetEnv
已挂起,但事实并非如此.(has hung, but this is not the case.)
使用SetEnv(Using SetEnv)
SetEnv
非常易于使用,只有几个命令行参数.本节将描述如何使用它们.打字(is very easy to use and has only a few command line arguments. This section will describe how to use them. Typing) SetEnv
在命令提示符下,然后按回车键将使(at a command prompt and then pressing the Return key will make) SetEnv
显示其使用信息.可以在此页面顶部的屏幕截图中看到.(display its usage information. This can be seen in the screenshot at the top of this page.)
用户环境变量(User Environment Variables)
从1.04版开始,(As of version 1.04,) SetEnv
还支持用户环境变量.如果你想(also supports User environment variables. If you want) SetEnv
添加或删除用户变量,然后添加(to either add or delete a User variable, then add the) -u
命令行选项.(option to the command line.)
创建一个变量(Creating a Variable)
SetEnv
可以创建两种类型的变量,一种简单的变量只有一个值,例如:(can create two types of variables, a simple one that has only a single value such as:)
InstallPath = C:\Program Files\Xanya\_Bin
…或更复杂的具有多个值的变量.一个很好的例子是(… or more complex variables with multiple values. A good example of this is the) PATH
变量:(variable:)
PATH = C:\WINDOWS\system32;C:\WINDOWS;C:\Program Files\Xanya\_Bin
要创建一个简单的变量,只需在以下命令行中输入即可.显然,替换您自己的变量名称和值:(To create a simple variable, just enter in the following command line. Obviously, substitute your own variable name and value:)
SetEnv -a InstallPath "C:\Program Files\Xanya\_Bin"
同样,要添加具有多个值的变量,您需要为每个值键入以下命令行.确保在值前加上(Similarly, to add a variable with multiple values, you need to type the following command line for each value. Ensure that you prefix the value with the) %
字符:(character:)
SetEnv -a name %value
E.g.
SetEnv -a PATH %"C:\Program Files\Xanya\_Bin
SetEnv -a PATH %"C:\Bin
要添加用户变量而不是系统变量,请添加(To add a User variable instead of a System one, add the) -u
选项,如以下示例所示:(option as in the following example:)
SetEnv -ua PATH %"C:\Program Files\Xanya\_Bin
修改现有变量(Modifying an Existing Variable)
修改变量的方式与创建变量的方式完全相同.如果您要修改多值变量,而忘记了(You modify variables in exactly the same way in which you would create them. If you are modifying a multi-value variable and you forget the) %
值的前缀,然后(prefix to the value, then) SetEnv
将自动检测到目标具有多个值,并将正确对其进行修改.(will automatically detect that the destination has multiple values and will modify it correctly.)
动态变量展开(Dynamic Variable Expansion)
这使您可以将变量设置为始终等于现有环境变量的值,即使该变量发生更改也是如此.让我向您展示一个何时会非常有用的示例.这个例子的灵感来自(This allows you to set a variable to always equal the value of an existing environment variable, even if that variable changes. Let me show you an example of when this would be very useful. This example was inspired by) 新技术(Synetech) ;如果您介意我偷了它,那就告诉我.(; if you mind me stealing it just let me know.)
假设您有一个名为(Say that you have an environment variable called) MEDIADIR
(设置((set to)C:\ Media(C:\Media)),并且您要创建另外两个名为() and you want to create two additional variables called) MUSICDIR
和(and) PICDIR
(设置((set to)%MEDIADIR%\音乐(%MEDIADIR%\Music)和(and)%MEDIADIR%\ Pics(%MEDIADIR%\Pics), 分别).这些将由操作系统扩展为以下内容:(, respectively). These would be expanded by the operating system to the following:)
C:\Media\Music
C:\Media\Pics
现在,假设您决定移动(Now, say you decided to move the) MEDIADIR
要驱动的文件夹(folder to drive)d(D)((()D:\ Media(D:\Media))使用动态变量展开.() using dynamic variable expansion.) SetEnv
将确保您的(will ensure that your) MUSICDIR
和(and) PICDIR
变量会自动正确地重定位到以下内容.(variables are correctly relocated to the following automatically.)
D:\Media\Music
D:\Media\Pics
要启用动态变量扩展,只需按照以下示例创建变量即可.使用(To enable dynamic variable expansion, simply create your variables as in the following example. Use the) ~
标识现有变量名称的符号.(symbol to identify the existing variable name.)
setenv -a MEDIADIR C:\Media
// Create the original variable
// Create our dynamic variable, when MEDIADIR changes, so will MUSICDIR
setenv -a MUSICDIR ~MEDIADIR~/Music
// Our second dynamic variable, PICDIR will change if MEDIADIR changes
setenv -a PICDIR ~MEDIADIR~/Pics
删除变量(Deleting a Variable)
要删除变量,请指定(To delete a variable, specify the) -d
选项而不是(option instead of the) -a
选项,如以下示例所示:(option, as in the following example:)
SetEnv -d InstallPath
要从多值变量中删除值,只需输入以下内容,并指定要删除的值:(To delete a value from a multi-value variable, you simply enter the following, specifying the value to remove:)
SetEnv -d PATH %"C:\Program Files\Xanya\_Bin
与修改多值变量一样,如果您忘记指定(As with modifying a multi-value variable, if you forget to specify the) %
前缀,然后(prefix, then) SetEnv
将自动解决此问题并仅删除指定的值.要删除用户环境变量,只需添加(will automatically work this out and delete the specified value only. To delete a User environment variable, simply add the) -u
选项如下:(option as follows:)
SetEnv -ud PATH %"C:\Program Files\Xanya\_Bin
从批处理文件运行SetEnv(Running SetEnv from a Batch File)
SetEnv
可以从批处理文件成功运行.但是,David Langford发现了一个问题,当您尝试指定扩展变量前缀时会发生(can be run successfully from a batch file. However, a problem was found by David Langford, which occurs when you attempt to specify the expanded variable prefix) %
如以下示例所示,将其设置为包含驱动器号的值.请注意,这只是运行时的一个问题(to a value which contains a drive letter, as in the following example. Note that this is only an issue when running) SetEnv
从批处理文件中:(from a batch file:)
SetEnv -d PATH %C:\Test
Windows批处理文件解释器将解释(The Windows batch file interpreter will interpret the) %
字符作为变量的前缀.它将用它认为是变量的字符替换字符(character as a prefix to a variable. It will replace the character with what it believes is the variable) %C:
带有该变量的内容,那当然是什么.这导致(with the contents of that variable, which of course is nothing. This results in) SetEnv
传递修改值,如下所示:(being passed a modified value, as shown here:)
SetEnv -d PATH \Test
这显然是错误的,并且(This is obviously wrong, and) SetEnv
将无法匹配扩展变量中要删除的值.因此,它将无法从变量中删除该值.解决方法是逃避(will not be able to match the value to be removed in the expanded variable. It will thus fail to delete the value from the variable. The workaround is to escape the) %
与另一个角色(character with another) %
字符.以下示例将正确删除(character. The following example will correctly remove the)**C:\测试(C:\Test)**扩展变量的值(value from the expanded variable,) PATH
.(.)
SetEnv -d PATH %%C:\Test
虫子(Bugs)
没有已知的错误,但是如果您发现任何错误,请告诉我.(There are no known bugs, but if you do find any, then please let me know.)
历史(History)
- 1.09(2008年2月9日)-修复了Windows 98有时无法打开Windows 2000的问题.(1.09 (Feb 9, 2008) - Fixed a problem on Windows 98 where it sometimes failed to open the)**自动执行程序(Autoexec.bat)**文件(由(file (Found by) linux(mulinux) ).().)
- 1.08(2007年5月31日)-更新(1.08 (May 31, 2007) - Updated)
SetEnv
有关如何使用删除用户环境变量的用法信息(’s usage information on how to delete a user environment variable using)SetEnv
.代码未更改.(. The code has not changed.) - 1.07(2007年1月25日)-修复了由发现的错误(1.07 (Jan 25, 2007) - Fixed a bug found by) 去骨(depaolim)
- 1.06(2007年1月14日)-添加了动态扩展支持(与使用(1.06 (Jan 14, 2007) - Added dynamic expansion support (same as using)
~
与(with)setx
),最初是由Andre Amaral请求的,另外由(), originally requested by Andre Amaral, additional request by) 新技术(Synetech) .(.) - 1.05(2006年9月6日)-添加了对在扩展前添加(而不是附加)值的支持(1.05 (Sep 06, 2006) - Added support to prepend (rather than append) a value to an expanded)
string
, 被要求(, requested by) 马苏亚(Masuia) .(.) - 1.04(2006年5月30日)-添加了对用户环境变量的支持.(1.04 (May 30, 2006) - Added support for User environment variables.)
- 1.03(2006年5月17日)-更新了文章以说明如何使用(1.03 (May 17, 2006) - Updated the article to explain how to use)
SetEnv
大卫`兰福德(David Langford)发现问题后,从批处理文件中删除该文件.(from a batch file, after a problem was found by David Langford.) - 1.03(2006年4月20日)-中的错误修复(1.03 (Apr 20, 2006) - Bug fix in)
ProcessWinXP()
被发现(discovered by) 态度(attiasr) .(.) - 1.01(2005年11月15日)-修复了错误(1.01 (Nov 15, 2005) - Bug fix in)
IsWinME()
被发现(discovered by) 坦率的(frankd) .(.) - 1.00(2005年10月29日)-首次公开发行(1.00 (Oct 29, 2005) - Initial public release)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
VC7.1 C++ VC7.0 WinXP Windows Win2K Visual-Studio VS.NET2003 Dev 新闻 翻译