在Inno Setup中避免"无法扩展shell文件夹常量userdocs"错误

And*_*ice 4 windows installation inno-setup

我将一些示例文档安装到Windows上标准"我的文档"文件夹的"PerfectTablePlan"子文件夹中.这适用于99%以上的用户.但是如果用户没有"我的文档"文件夹,我会收到以下形式的一些丑陋的错误消息:

内部错误:无法扩展shell文件夹常量"userdocs"

这对用户来说不是很有信心!

不为这些用户安装样本(或将其安装在其他地方)是可以接受的.但不要显示丑陋的错误消息.

问题似乎来自{userdocs}的ExpandConstant宏扩展.

有没有什么方法可以在不使用宏的情况下获得"我的文档"的路径?

或者某种方式来抑制错误信息?ExpandConstant抛出异常:http://www.jrsoftware.org/ishelp/index.php?topic = isxfunc_expandconstant

我的.iss文件的相关部分如下所示:

#define MySampleDir "{code:SampleDirRoot}\PerfectTablePlan"
...
[Files]
Source: ..\binaries\windows\program\plans\*_v14.tp; DestDir: {#MySampleDir}\; Flags: ignoreversion onlyifdoesntexist createallsubdirs recursesubdirs uninsneveruninstall;
Source: ..\binaries\windows\program\plans\*_v3.tps; DestDir: {#MySampleDir}\; Flags: ignoreversion onlyifdoesntexist createallsubdirs recursesubdirs uninsneveruninstall;
...
[Code]
function SampleDirRoot(Param: String): String;
begin
if DirExists( ExpandConstant('{userdocs}') ) then
Result := ExpandConstant('{userdocs}')
else
Result := ExpandConstant('{allusersprofile}')
end;
Run Code Online (Sandbox Code Playgroud)

TLa*_*ama 5

例外:

无法扩展shell文件夹常量'常量名称'

当内部调用SHGetFolderPath函数(ExpandConstant在扩展shell文件夹常量时从内部调用)返回给定文件夹CSIDL的空路径字符串时引发,在本例中为CSIDL_PERSONAL标识符.

这意味着用户没有该CSIDL_PERSONAL文件夹.这让我想知道如何配置Windows的用户帐户没有该文件夹.那么,您可以通过捕获try..except块中引发的内部异常来解决此问题(或Windows错误配置?):

[Code]
function SampleDirRoot(Param: string): string;
var
  Folder: string;
begin
  try
    // first try to expand the {userdocs} folder; if this raises that
    // internal exception, you'll fall down to the except block where
    // you expand the {allusersprofile}
    Folder := ExpandConstant('{userdocs}');
    // the {userdocs} folder expanding succeded, so let's test if the
    // folder exists and if not, expand {allusersprofile}
    if not DirExists(Folder) then
      Folder := ExpandConstant('{allusersprofile}');
  except
    Folder := ExpandConstant('{allusersprofile}');
  end;
  // return the result
  Result := Folder;
end;
Run Code Online (Sandbox Code Playgroud)

但我从来没有听说过没有CSIDL_PERSONAL文件夹的可能性.请注意,上述代码仅保护{userdocs}常量.

  • 有些IT人员似乎没有"我的文档"文件夹来配置工作计算机.大概是强迫他们将文档存储在网络驱动器上. (2认同)