TIniFile.WriteString(Section,Key,Value)自动更改值

时间:2014-04-07 15:34:59

标签: lazarus tstringlist

当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<>B (A=["abcde"] but B=[abcde])

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

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

2 个答案:

答案 0 :(得分: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.

答案 1 :(得分:0)

找到答案的努力太多了。

<强> TCustomIniFile.StripQuotes

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