|
| 1 | +// path.iss - Manipulate the PATH variable |
| 2 | +// Copyright (c) Chris Hyndman |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +[Code] |
| 17 | +
|
| 18 | +const |
| 19 | + PathVarRegRoot = HKEY_LOCAL_MACHINE; |
| 20 | + PathVarRegPath = 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment'; |
| 21 | + |
| 22 | +function ContainsPath(Dir: String): Boolean; |
| 23 | +var |
| 24 | + RegValue: String; |
| 25 | +begin |
| 26 | + Result := False; |
| 27 | + if Dir <> '' then begin |
| 28 | + RegQueryStringValue(PathVarRegRoot, PathVarRegPath, 'Path', RegValue); |
| 29 | + if Pos(Dir, RegValue) <> 0 then |
| 30 | + Result := True; |
| 31 | + end; |
| 32 | +end; |
| 33 | +
|
| 34 | +// Given a directory path, appends the directory to the system's Path environment variable, |
| 35 | +// if it doesn't already exist |
| 36 | +procedure AppendPath(Dir: String); |
| 37 | +var |
| 38 | + RegValue: String; |
| 39 | +begin |
| 40 | + if Dir <> '' then begin |
| 41 | + RegQueryStringValue(PathVarRegRoot, PathVarRegPath, 'Path', RegValue); |
| 42 | + if Pos(Dir, RegValue) = 0 then begin |
| 43 | + RegWriteStringValue(PathVarRegRoot, PathVarRegPath, 'Path', RegValue + ';' + Dir); |
| 44 | + end; |
| 45 | + end; |
| 46 | +end; |
| 47 | +
|
| 48 | +// Given a directory path, deletes the directory from the system's Path environment variable, |
| 49 | +// if it exists |
| 50 | +procedure DeletePath(Dir: String); |
| 51 | +var |
| 52 | + RegValue: String; |
| 53 | + DirIdx: Integer; |
| 54 | +begin |
| 55 | + if Dir <> '' then begin |
| 56 | + RegQueryStringValue(PathVarRegRoot, PathVarRegPath, 'Path', RegValue); |
| 57 | + DirIdx := Pos(Dir, RegValue); |
| 58 | + if DirIdx <> 0 then begin |
| 59 | + if DirIdx = 1 then begin |
| 60 | + Delete(RegValue, 1, Length(Dir) + 1); |
| 61 | + end else begin |
| 62 | + Delete(RegValue, DirIdx - 1, Length(Dir) + 1); |
| 63 | + end; |
| 64 | + RegWriteStringValue(PathVarRegRoot, PathVarRegPath, 'Path', RegValue); |
| 65 | + end; |
| 66 | + end; |
| 67 | +end; |
0 commit comments