【问题标题】:TIniFile.WriteString(Section,Key,Value) changed value automaticallyTIniFile.WriteString(Section,Key,Value) 自动更改值
【发布时间】:2014-04-07 15:34:58
【问题描述】:

当 Value 是带引号的字符串时,引号 (") 会被自动删除。

这意味着,以下两个语句, A.WriteString('Section','Key','"abcde"')A.WriteString('Section','Key','abcde') 没有什么不同。

请看我的代码(很清楚):

program project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes, IniFiles, sysutils
  { you can add units after this };

var
  List: TIniFile;
  A, B: String;

begin
  List := TIniFile.Create('file.ini');
  A := '"abcde"';
  List.WriteString('Section', 'Key', A);
  List.Free;

  List := TIniFile.Create('file.ini');
  B := List.ReadString('Section', 'Key', '');
  List.Free;

  if A<>B then raise Exception.Create(Format('A<>B (A=[%s] but B=[%s])', [A, B]));
end.

前面的代码引发以下异常:A&lt;&gt;B (A=["abcde"] but B=[abcde])

我想编写如下代码:A.WriteString('Section', 'Key', List.CommaText); 因为List.CommaText 可能是带引号的字符串,所以我没有解决上述代码的方法。

是错误还是功能?如何将TStrings 保存到TIniFile

【问题讨论】:

    标签: lazarus tstringlist


    【解决方案1】:

    在编写之前,将引号(如果有的话)换成别的东西,保证不会出现在字符串中。阅读后,将其改回引号。例如:

    begin
      List := TIniFile.Create('file.ini');
      A := '"abcde"';
      List.WriteString('Section', 'Key', ReplaceStr(A, '"', #1));
      List.Free;
    
      List := TIniFile.Create('file.ini');
      B := ReplaceStr(List.ReadString('Section', 'Key', ''), #1, '"');
      List.Free;
    
      if A<>B then raise Exception.Create(Format('A<>B (A=[%s] but B=[%s])', [A, B]));
    end.
    

    【讨论】:

    • 你能扩展一下吗?
    • 好吧,然后将其放入您的答案中,这样会更清楚:)
    【解决方案2】:

    费了很大力气才找到答案。

    TCustomIniFile.StripQuotes

    http://www.freepascal.org/docs-html/fcl/inifiles/tcustominifile.stripquotes.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-18
      • 1970-01-01
      • 2015-11-22
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      相关资源
      最近更新 更多