【问题标题】:Inno Setup Code section create hidden fileInno Setup Code 部分创建隐藏文件
【发布时间】:2014-05-06 22:43:51
【问题描述】:

我正在处理一个 Inno Setup 项目。该项目正在使用[Code] 部分中的SaveStringToFile 函数写出一个文件。我想将此文件设置为隐藏的系统文件,但我无法找到有关如何使其工作的信息。有什么想法吗?

【问题讨论】:

    标签: inno-setup pascalscript


    【解决方案1】:

    Inno Setup Pascal Script 中没有设置文件属性的功能。因此,您要么必须导入可以设置文件属性的 Windows API 函数,要么使用以下技巧。您可以创建一个空文件,将其设置为隐藏在脚本条目中,之后您可以在那里编写您需要的任何内容,因此安装过程将为您创建一个隐藏文件:

    [Files]
    ; MyFile.txt is an empty text file
    Source: "MyFile.txt"; DestDir: "{app}"; Attribs: hidden; AfterInstall: WriteToFile
    
    [Code]
    procedure WriteToFile;
    begin
      SaveStringToFile(ExpandConstant('{app}\MyFile.txt'), 'Hello!', True);
    end;
    

    为了完整起见,我还包括一个函数,您可以通过该函数将隐藏属性显式设置为文件:

    [Code]
    #ifdef UNICODE
      #define AW "W"
    #else
      #define AW "A"
    #endif
    
    const
      INVALID_FILE_ATTRIBUTES = $FFFFFFFF;
    
    function GetFileAttributes(lpFileName: string): DWORD;
      external 'GetFileAttributes{#AW}@kernel32.dll stdcall';
    function SetFileAttributes(lpFileName: string; dwFileAttributes: DWORD): BOOL;
      external 'SetFileAttributes{#AW}@kernel32.dll stdcall';
    
    procedure RaiseLastOSError;
    var
      LastError: LongInt;
    begin
      LastError := DLLGetLastError;
      RaiseException(Format('System Error. Code: %d. %s', [LastError,
        SysErrorMessage(LastError)]));
    end;
    
    procedure SetFileHiddenAttr(const FileName: string);
    var
      Attrs: DWORD;
    begin
      Attrs := GetFileAttributes(FileName);
      if Attrs <> INVALID_FILE_ATTRIBUTES then
      begin
        if Attrs and FILE_ATTRIBUTE_HIDDEN = 0 then
          if not SetFileAttributes(FileName, Attrs or FILE_ATTRIBUTE_HIDDEN) then
            RaiseLastOSError;
      end
      else
        RaiseLastOSError;
    end;
    

    【讨论】:

      猜你喜欢
      • 2016-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-04
      • 2016-10-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多