我需要在inno安装安装脚本中设置python路径。
据我所知,从命令行设置它是通过输入:
来完成的set path= C:\pythonTargetDir;%path%
在我的inno设置中,在[run]部分我尝试添加以下行:
Filename: "set"; Parameters: " ""path ={#pythonTargetDir}"" TARGETDIR=""{#pythonTargetDir}"" ALLUSERS=1 "; StatusMsg: "Adding Python to system path..."
运行脚本在以下错误消息框中结束:
无法执行文件: 设置
CreateProcess失败;代码2; 系统找不到指定的文件
我想我可以编写一个批处理文件来执行set命令,我会命令inno从运行部分调用它,这样就可以了。但即使它能起作用,我也不想那样做。似乎不是很优雅的解决方案。还有另一种方式吗?
[ini]和[Registry]部分似乎也是正确的。
答案 0 :(得分:1)
我在pascal中的功能很少。查看代码并确定它们是否符合您的需求。我很久以前就做过了,而且不是因为版本更好。 How do I modify the PATH environment variable when running an Inno Setup Installer?是很好的解决方案。
获取数组中的路径:
function GetEnvStrings(VarName:string;AllUsers:Boolean):TArrayOfString;
var
Path:string;
i:Longint;
p:Integer;
begin
Path:='';
// See http://www.jrsoftware.org/isfaq.php#env
if AllUsers then
begin
RegQueryStringValue(HKEY_LOCAL_MACHINE,'SYSTEM\CurrentControlSet\Control\Session Manager\Environment',VarName,Path);
end
else
begin
RegQueryStringValue(HKEY_CURRENT_USER,'Environment',VarName,Path);
end;
Path:=Path+';';
i:=0;
SetArrayLength(Result,0);
p:=Pos(';',Path);
while p>0 do
begin
SetArrayLength(Result,i+1);
if p>1 then
begin
Result[i]:=Copy(Path,1,p-1);
i:=i+1;
end;
Path:=Copy(Path,p+1,Length(Path));
p:=Pos(';',Path);
end;
end;
检查路径中是否已有dir:
function NeedsAddPath(Param: string): boolean;
var
OrigPath: string;
ok: Boolean;
begin
ok:= False;
if not RegQueryStringValue(HKEY_LOCAL_MACHINE, 'SYSTEM\CurrentControlSet\Control\Session Manager\Environment', 'Path', OrigPath) then
begin
ok:= True;
exit;
end;
ok:= Pos(';' + Param + ';', ';' + OrigPath + ';') = 0;
Result:= ok;
end;
设置路径1:
function SetEnvStrings(VarName:string; AllUsers,DeleteIfEmpty: Boolean; DirStrings:TArrayOfString):Boolean;
var
Path,KeyName:string;
i:Longint;
begin
Path:='';
for i:=0 to GetArrayLength(DirStrings)-1 do
begin
if Length(DirStrings[i])>0 then
begin
if Length(Path)>0 then
begin
Path:=Path+';'+DirStrings[i];
end
else
begin
Path:=DirStrings[i];
end;
end;
end;
Log(Path);
if AllUsers then
begin
Log('All');
KeyName:='SYSTEM\CurrentControlSet\Control\Session Manager\Environment';
if DeleteIfEmpty and (Length(Path)=0) then
begin
Result:=(not RegValueExists(HKEY_LOCAL_MACHINE,KeyName,VarName))
or RegDeleteValue(HKEY_LOCAL_MACHINE,KeyName,VarName);
end
else
begin
Log(VarName + ' All');
RegWriteStringValue(HKEY_LOCAL_MACHINE,KeyName,VarName,Path);
KeyName:='SYSTEM\ControlSet2002\Control\Session Manager\Environment';
RegWriteStringValue(HKEY_LOCAL_MACHINE,KeyName,VarName,Path);
end;
end
else
begin
KeyName:='Environment';
Log(KeyName);
if DeleteIfEmpty and (Length(Path)=0) then
begin
Result:=(not RegValueExists(HKEY_CURRENT_USER,KeyName,VarName))
or RegDeleteValue(HKEY_CURRENT_USER,KeyName,VarName);
end
else
begin
Log(VarName + ' Environment');
Result:=RegWriteStringValue(HKEY_CURRENT_USER,KeyName,VarName,Path);
end;
end;
end;
设置路径2:
procedure SetPath(pEnvPath: String);
var
Msg :String;
EnvPath: TArrayOfString;
i: integer;
begin
EnvPath:=GetEnvStrings('PATH', IsAdminLoggedOn);
if NeedsAddPath(pEnvPath) then
begin
i:=GetArrayLength(EnvPath);
SetArrayLength(EnvPath,i+1);
log(pEnvPath);
EnvPath[i]:= pEnvPath;
end;
if not SetEnvStrings('PATH', True, True, EnvPath) then
begin
Msg:='Line {#__LINE__}: Unable to set the PATH environment variable.';
Log(Msg);
end;
if not SetEnvStrings('PATH', False, True, EnvPath) then
begin
Msg:='Line {#__LINE__}: Unable to set the PATH environment variable.';
Log(Msg);
end;
end;