2014-04-07 26 views
-1

当值是一个引用字符串,引号(“)被自动删除。TIniFile.WriteString(科键,值)改变值自动

这意味着,下面的语句二, 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. 

上面的代码提高FOL下降的例外:A<>B (A=["abcde"] but B=[abcde])

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

它是一个错误或功能?如何将TStrings保存到TIniFile

回答

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

那么,继续前进,并将其放入你的答案,所以它更清晰:) –