设置环境变量在Windows 9x,NT,2000或XP

 

[Code]
// Version log:
// 09/22/2005: Initial release (axis23(at)gmail.com)
// Based in 'Native ISX procedures for PATH modification' from Thomas Vedel ('ModifyPath.iss')
const
// Modification method
vmAddOnlyIfVarDoesNotExists = $1; // Add Var only is this Var doesn't exists
vmAddAllways = $2; // Add Var allways
// Scope
vsCurrentUser = 1; // Add Var for current user
vsAllUsers = 2; // Add Var for all users
// Error results
mvOK = 0; // No errors
mvMissingRights = -1; // User has insufficient rights
mvAutoexecNoWriteacc = -2; // Autoexec can not be written (may be readonly)
mvBothMethods = -3; // Error if invoque function with two methods
{ Helper function: Modify Var on Windows 9x }
function ModifyVar9x(VarName, VarValue: string; Method: integer): integer;
var
AutoexecLines: TStringList;
ActualLine: String;
VarLineNos: TStringList;
FirstVarLineNo: Integer;
VarExists: boolean;
LineNo, CharNo, Index: integer;
TempString: String;
TempVarName: String;
TempVarName_comp: String;
begin
// Expect everything to be OK
result := mvOK;
// Create stringslists
AutoexecLines := TStringList.Create;
VarLineNos := TStringList.Create;
TempVarName := 'SET ' + uppercase(VarName) + '=';
TempVarName_comp := 'SET' + uppercase(VarName) + '=';
// Create VarExists
VarExists := false;
// Read existing Var
LoadStringFromFile('c:Autoexec.bat', TempString);
AutoexecLines.Text := TempString;
VarLineNos.Clear;
// Read Autoexec line by line
for LineNo := 0 to AutoexecLines.Count - 1 do begin
ActualLine := AutoexecLines.Strings[LineNo];
// Check if line starts with 'Varname=' after first stripping spaces and other "fill-chars"
if Pos('=', ActualLine) > 0 then
begin
for CharNo := Pos('=', ActualLine)-1 downto 1 do
if (ActualLine[CharNo]=' ') or (ActualLine[CharNo]=#9) then
Delete(ActualLine, CharNo, 1);
if Pos( '@', ActualLine) = 1 then
Delete(ActualLine, 1, 1);
if (Pos(TempVarName_Comp, uppercase(ActualLine))=1) then
begin
ActualLine := TempVarName + VarValue;
// Update list of line numbers holding VarName variables
VarLineNos.Add(IntToStr(LineNo));
VarExists :=true;
end;
end;
end;
// Save first line number in Autoexec.bat which modifies Var environment variable
if VarLineNos.Count > 0 then
FirstVarLineNo := StrToInt(VarLineNos.Strings[0])
else
FirstVarLineNo := 0;
// Only change autoexec if method permit this
if ((Method = vmAddAllways) or ((Method = vmAddOnlyIfVarDoesNotExists) and (VarExists=false))) then
begin
// Write Modified Var back to Autoexec.bat
// First delete all existing Var references from Autoexec.bat
Index := VarLineNos.Count-1;
while (Index>=0) do
begin
AutoexecLines.Delete(StrToInt(VarLineNos.Strings[Index]));
Index := Index-1;
end;
// Then insert new Var variable into Autoexec.bat
if(VarExists=false) then
FirstVarLineNo := AutoexecLines.Count;
AutoexecLines.Insert(FirstVarLineNo, TempVarName+VarValue);
// Delete old Autoexec.bat from disk
if not DeleteFile('c:Autoexec.bat') then
result := mvAutoexecNoWriteAcc;
Sleep(500);
// And finally write Autoexec.bat back to disk
if not (result=mvAutoexecNoWriteAcc) then
SaveStringToFile('c:Autoexec.bat', AutoexecLines.Text, false);
end;
// Free stringlists
VarLineNos.Free;
AutoexecLines.Free;
end; // ModifyVar9x

{ Helper function: Modify Var on Windows NT, 2000 and XP }
function ModifyVarNT(VarName, VarValue: string; Method, Scope: integer): integer;
var
RegRootKey: integer;
RegSubKeyName: string;
RegValueName: string;
ResultVar: string;
OK: boolean;
begin
// Expect everything to be OK
result := mvOK;
// Initialize registry key and value names to reflect if changes should be global or local to current user only
case Scope of
vsCurrentUser:
begin
RegRootKey := HKEY_CURRENT_USER;
RegSubKeyName := 'Environment';
RegValueName := VarName;
end;
vsAllUsers:
begin
RegRootKey := HKEY_LOCAL_MACHINE;
RegSubKeyName := 'SYSTEMCurrentControlSetControlSession ManagerEnvironment';
RegValueName := VarName;
end;
end;
// Read current Var value from registry
OK := RegQueryStringValue(RegRootKey, RegSubKeyName, RegValueName, ResultVar);
// Write new Var value to registry
if ((Method = vmAddAllways) or ((Method = vmAddOnlyIfVarDoesNotExists) and (OK=false))) then
begin
if not RegWriteStringValue(RegRootKey, RegSubKeyName, RegValueName, VarValue) then
begin
result := mvMissingRights;
Exit;
end;
end;
end; // ModifyVarNT

{ Main function: Modify Var }
function ModifyVar(VarName, VarValue: string; Method, Scope: integer): integer;
begin
// Check if both add and remove has been specified (= error!)
if (Method and (vmAddOnlyIfVarDoesNotExists and vmAddAllways)) > 0 then
begin
result := mvBothMethods;
Exit;
end;
// Test if Win9x
if InstallOnThisVersion('4,0','0,0') = irInstall then
ModifyVar9x(VarName,VarValue, Method);
// Test if WinNT, 2000 or XP
if InstallOnThisVersion('0,4','0,0') = irInstall then
ModifyVarNT(VarName, VarValue, Method, Scope);
end; // ModifyVar

你可能感兴趣的:(职场,休闲,nt,设置环境变量在Windows,9x,2000或XP)