修改路径是基本的字符串操作:您可以从注册表中读取当前路径,对其进行操作以满足您的需求,然后写回去。
您可能可以编写一个Inno Setup脚本函数,这样您就没有任何外部依赖项。或者编写一个Delphi DLL,用于从Inno Setup的脚本中使用,这样更容易调试。
编辑
这是我实际在生产中使用的例程的修改版本。它将从“搜索路径”注册表值或“浏览路径”(任何其他路径)读取整个路径列表,可能会删除一些路径,并添加一些路径(如果它们不存在)。
procedure UpdateDelphiPaths(const RegistryKey, RegistryValue: string; PathsToRemove, PathsToAdd: TStrings);
var R:TRegistry;
SKeys:TStringList;
Found:Boolean;
Updated:Boolean;
i,j:Integer;
s:string;
R_Globals:TRegistry;
function PrepPathForComparasion(const Path:string):string;
begin
if Path = '' then Result := '\'
else
if Path[Length(Path)] = '\' then
Result := LowerCase(Path)
else
Result := LowerCase(Path) + '\';
end;
function PathMatchesRemoveCriteria(const Path:string): Boolean;
var i:Integer;
begin
for i:=0 to PathsToRemove.Count-1 do
if AnsiPos(PathsToRemove[i], Path) <> 0 then
Exit(True);
Result := False;
end;
begin
R := TRegistry.Create;
try
R.RootKey := HKEY_CURRENT_USER;
if R.OpenKey(RegistryKey + '\Library', False) then
if R.ValueExists(RegistryValue) then
begin
SKeys := TStringList.Create;
try
SKeys.Delimiter := ';';
SKeys.StrictDelimiter := True;
SKeys.DelimitedText := R.ReadString(RegistryValue);
Updated := False;
for i:=0 to PathsToAdd.Count-1 do
begin
Found := False;
for j:=0 to SKeys.Count-1 do
if LowerCase(Trim(SKeys[j])) = LowerCase(Trim(PathsToAdd[i])) then
Found := True;
if not Found then
begin
SKeys.Add(PathsToAdd[i]);
Updated := True;
end;
end;
i := 0;
while i < SKeys.Count do
begin
if PathMatchesRemoveCriteria(SKeys[i]) then
begin
Found := False;
for j:=0 to PathsToAdd.Count-1 do
begin
if PrepPathForComparasion(SKeys[i]) = PrepPathForComparasion(PathsToAdd[j]) then
Found := True;
end;
if not Found then
begin
SKeys.Delete(i);
Updated := True;
end
else
Inc(i);
end
else
Inc(i);
end;
if Updated then
begin
s := SKeys[0];
for i:=1 to SKeys.Count-1 do
if SKeys[i] <> '' then
begin
s := s + ';' + SKeys[i];
end;
R.WriteString(RegistryValue, s);
R_Globals := TRegistry.Create;
try
R_Globals.OpenKey(RegistryKey + '\Globals', True);
R_Globals.WriteString('ForceEnvOptionsUpdate', '1');
finally R_Globals.Free;
end;
end;
finally SKeys.Free;
end;
end;
finally R.Free;
end;
end;
我可以使用Delphi代码调用这个例程,以确保给定库的最新搜索路径已安装:
var ToRemove, ToAdd: TStringList;
begin
ToRemove := TStringList.Create;
try
ToAdd := TStringList.Create;
try
ToRemove.Add('LibraryName\Source');
ToAdd.Add('C:\LibraryName\Source');
UpdateDelphiPaths('Software\CodeGear\BDS\7.0', 'Test Path', ToRemove, ToAdd);
finally ToAdd.Free;
end;
finally ToRemove.Free;
end;
end;
注意要删除和添加的
ToRemove
和
ToAdd
。我可以安全地在删除和添加列表中指定搜索路径:只有当路径符合“Remove”标准但不在“ToAdd”列表中时,才会删除该路径。还请注意
PathMatchesRemoveCriteria
函数。
您可能可以修改代码,使其直接从InnoScript中工作,或者将代码放入DLL并从安装程序中使用该DLL。 DLL变体具有在Delphi中轻松调试并对Inno自身非常友好的优点; Inno变体则具有没有外部依赖项的优点,但需要适应和调试代码。