Inno Setup will not create folder under C:\\Users\

2019-05-04 20:11发布

问题:

I am using Inno Setup to build my installer and I have the C:\Users\Public folder hardcoded in my [Files] section to place some files (Inno Setup does not have a constant for this folder)

My goal is to have the install create a C:\Users\Public\MyApp folder with some files in it. However when I run the install, it is creating the folder here: C:\Users\Public\Public Documents\MyApp

Is this a permissions issue where the installer doesn't have access to create a folder directly under C:\Users\Public?

[Files]
Source: "MyApp\db.mdf"; DestDir: "{drive:{src}}\Users\Public\MyApp"; Flags: ignoreversion;

回答1:

I cannot reproduce your problem. For me your code works. I've tested it on Windows Vista, 7 and 10. It always installs to C:\Users\Public\MyApp.


Though I do not understand the {drive:{src}}. How does the drive of the Users folder relate to the drive of the installer? You should use the {sd} constant:

[Files]
Source: "MyApp\db.mdf"; DestDir: "{sd}\Users\Public\MyApp"; Flags: ignoreversion

But anyway, to resolve the path to the C:\Users\Public, you can use the PUBLIC environment variable:

[Files]
Source: "MyApp\db.mdf"; DestDir: "{%PUBLIC}\MyApp"; Flags: ignoreversion

It works since Windows Vista.

Alternatively, you can use SHGetKnownFolderPath with FOLDERID_Public. For an example code, see Constant for AppData\LocalLow?


If you need to support even Windows XP, where there is no C:\Users\Public folder or PUBLIC variable, you have to find out, what path your need to use there instead (probably C:\Documents and Settings\All Users), and implement a fallback using a scripted constant:

[Files]
Source: "MyProg.exe"; DestDir: "{code:GetPublicPath}\MyApp"; Flags: ignoreversion

[Code]

function GetPublicPath(Param: string): string;
begin
  Result := GetEnv('PUBLIC');
  if Result <> '' then
  begin
    Log(Format('PUBLIC is "%s"', [Result]));
  end
    else
  begin
    Result := GetEnv('ALLUSERSPROFILE');
    Log(Format('PUBLIC is not set, ALLUSERSPROFILE is "%s"', [Result]));
  end;
end;

And for others, it's worth noting that your need for resolve C:\Users\Public is very specific, related to this question: C++ app MDB in ProgramData copies to user's AppData folder when I dont want it to.

One usually does not want the C:\Users\Public, but C:\Users\Public\Documents (= {commondocs}) or C:\ProgramData aka C:\Users\All Users (= {commonappdata}).



标签: inno-setup