How to change defaultdirname parameter just before Install in Inno Setup?

橙三吉。 提交于 2020-01-03 14:02:28

问题


I want to change defaultdirname parameter in ssInstall part. How can I do that? Is there a function for setting [Setup] parameters.


回答1:


The following global objects are available : MainForm of type TMainForm, WizardForm of type TWizardForm and UninstallProgressForm of type TUninstallProgressForm and one special constant: crHand of type TControl.Cursor.

If you want to set the default directory in the wizard, just access to it's componants like you would in normal delphi code.

For example, set the directory to a custom value:

WizardForm.DirEdit.Text := 'c:\test';

to read that value you can use the WizardDirValue function.

I say 'just access'... but it took me an hour to figure out ;)




回答2:


There seems to be no way to change a script constant via scripting.
I think your best bet is to modify the target directory for each entry in the [Files] section, e.g.

[Files]
Source: "MYPROG.EXE"; DestDir: "{code:NewTargetDir}"

and derive your new installation directory like this:

[Code]
function NewTargetDir(Param: String): String;
begin
  Result := ExpandConstant('{app}') + '\MySubDir';
end;

Since the NewTargetDir function will be called just before the file is actually copied, this should work.

However, I think you should reconsider your approach. First asking the user to specify a directory to installinto, and then actually installing into a different directory, which seems to be your intent, is the wrong way, IMO. Do you really have a compelling reason to install into another directory than the one specified by the user? Besides, the result of my example code could just as well be achieved by specifying

[Files]
Source: "MYPROG.EXE"; DestDir: "{app}\MySubDir"

without any scripting needed. When in doubt, go for the simpler solution.




回答3:


I have a similar situation, where the setup app is receiving the install path from the command line. I'm using the solution proposed by Jonx:

WizardForm.DirEdit.Text := 'c:\test';

Example:

function CompareParameter(param, expected: String): Boolean;
begin
  Result := False;
  if Length(param) >= Length(expected) then
  begin
    if CompareText(Copy(param, 1, Length(expected)), expected) = 0 then
    begin
      Result := True;
    end;
  end;
end;

function GetParameter(expectedParam: String): String;
var
  i : LongInt;
begin
  Result := '';
  for i := 0 to ParamCount() do
  begin
    if CompareParameter(ParamStr(i), '/' + expectedParam + '=') then
    begin
      Result := Copy(ParamStr(i), Length(expectedParam) + 3, Length(ParamStr(i)));
      break;
    end;
  end;
end;

procedure InitializeWizard();
var
  newInstallFolder: String;
begin
  newInstallFolder := GetParameter('INSTALL_FOLDER');
  if Length(newInstallFolder) > 2 then
  begin
    if Copy(newInstallFolder, 1, 1) = '"' then
      newInstallFolder := Copy(newInstallFolder, 2, Length(newInstallFolder) - 2)
    if Length(newInstallFolder) > 1 then
      WizardForm.DirEdit.Text := newInstallFolder;
  end;
end;

The setup app is being started from another setup, in silent mode. It worked OK for me.



来源:https://stackoverflow.com/questions/1299336/how-to-change-defaultdirname-parameter-just-before-install-in-inno-setup

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!