2014-02-18 44 views
1

我可以使用Delphi的TIniFile類以任何方式將註釋寫入我的INI文件嗎?使用TiniFile編寫包含註釋行的INI文件

[VALUES] 
; first number 
a=5 
; something else 
b=25 
... 
... 

在實例(EXAMPLE),我發現在網絡上也有不錯的,有時註釋行補充,而不是展示瞭如何創建,讀,寫這幾行....

+1

我沒有看到你的鏈接例如任何評論。 –

回答

4

TIniFile類是一個鬆散的包裝提供INI文件服務的Windows API函數。該API不支持書寫評論,因此TIniFile也不能這樣做。

如果你想發表帶有註釋的文件,你必須找到一個不同的INI文件庫,或者自己推出。

4

無論TIniFileTMemIniFile支持讀取或寫入INI文件註釋。

如果您需要添加或檢索評論,則需要改爲使用TStringList,並使用NamesValues功能讀取和寫入值。您可以使用SaveToFile保存該文件並使用LoadFromFile加載該文件,但需要更多工作來自行處理默認值和各種ReadXX方法。

1

只能建議使用WriteString類似以下內容:

ini.WriteString('VALUES','; first number','');

結果不是你的預期完全一樣,但無論如何,你將能夠添加一些評論。

0

我知道它的一個老問題,但我會添加我的功能來爲ini文件添加評論。隨意改善。 對不起語法mistakes.English是不是我的主要語言

class Procedure TCoreIniAsTextFile.AddComment(ini: TiniFile; Comment: array of string; Section: string = ''; Key: string = ''); 
Const 
    LIST_NUMERIC_SIZE: Integer = 3; 
var 
    IniAsText   : Tstrings; 
    i     : Integer; 
    j     : Integer; 
    Buffer   : string; 
    // Method to remove previous comment 
    Procedure WriteComment(StartLine: Integer); 
var 
    Linha    : Integer; 
    LinBuf   : string; 
begin 
    repeat 
    // add a space to avoid a except in the following line 
    // if a line is blank 
    LinBuf := Trim(IniAsText[StartLine]) + ' '; 
    if LinBuf[1] = ';' then // is a comment 
     IniAsText.Delete(StartLine); 
    until (LinBuf[1] <> ';') or (IniAsText.Count = 0); 
    // add new comment 
    for Linha := High(Comment) downto Low(Comment) do 
    begin 
     IniAsText.Insert(StartLine, ';' + Comment[Linha]); 
    end; 

    IniAsText.Insert(StartLine, ''); // add a line for esthetic purposes 
    IniAsText.SaveToFile(ini.FileName); 
end; 
// function to add zeros to left 1 -> 001 
function StrZeroReg(num: Int64; nsize: Integer): string; 
begin 
    Result := IntToStr(num); 
    Result := StringOfChar('0', nsize - Length(Result)) + Result; 
end; 
begin 
    IniAsText := nil; // to avoid a warning below 
    Section := Uppercase(Section); 
    Key := Uppercase(Key); 
    try 
    IniAsText := TStringList.Create; 
    if not FileExists(ini.FileName) then 
     IniAsText.SaveToFile(ini.FileName); // Create a blank file 

    IniAsText.LoadFromFile(ini.FileName); 

    // If no section or key direct comment to file header 
    if (Section = EmptyStr) and (Key = EmptyStr) then 
     begin 
     WriteComment(0); 
     Exit; 
     end; 

    // If key is passed section is required 
    if (Section = EmptyStr) and (Key <> EmptyStr) then 
     begin 
     if not ini.SectionExists(Section) then 
      raise Exception.Create('Section is required to write key comment'); 
     end; 

    //If section is passed, but not key assume coment is to section 
    if (Section <> EmptyStr) and (Key = EmptyStr) then 
     begin 
     // section must exists 
     if not ini.SectionExists(Section) then 
      raise Exception.Create('Section ' + Section + ' does not exists'); 

     // search for section location in file 
     for i := 0 to IniAsText.Count - 1 do 
      begin 
      Buffer := Trim(Uppercase(IniAsText[i])); 
      if (Buffer = '[' + Section + ']') then 
       begin 
       WriteComment(i); 
       Exit; 
       end; 
      end; 

     // sanity check 
     raise Exception.Create('Section ' + Section + ' does not exists'); 
     exit; 
     end; 

    // If key and section is passed assume is a key comment 
    if not ini.ValueExists(Section, Key) then 
     begin 

     // some ini libraries save stringlists to ini and save values as 
     // valuea001,valuea002,...,valueannn . then search KEY000 as compatibility 
     if ini.ValueExists(Section, Key + StrZeroReg(0, LIST_NUMERIC_PALCES)) then 
      Key := Key + StrZeroReg(0, LIST_NUMERIC_PALCES) 
     else // if not found Key000 
      raise Exception.Create('KEY ' + Section + '\' + Key + ' does not exists'); 
     end; 

    // search combination of section + key 
    for i := 0 to IniAsText.Count - 1 do 
     begin 
     Buffer := Trim(Uppercase(IniAsText[i])); 
     if (Buffer = '[' + Section + ']') then 
      begin 
      for j := i + 1 to IniAsText.Count - 1 do 
       begin 
       Buffer := Trim(Uppercase(IniAsText[j])) + ' '; // space to avoid a exception 
       // Buffer[1] <> ';' to avoid commented lines 
       // Key + '=', Buffer) = 1 to avoid false cognats but will fail if param is 
       // param =value (valid to some libraries) 
       if (Buffer[1] <> ';') and (Pos(Key + '=', Buffer) = 1) then 
        begin 
        WriteComment(j); 
        Exit; 
        end; 
       end 
      end; 
     end; 

    finally 
    if Assigned(IniAsText) then 
     IniAsText.Free; 
    end; 
end; 
相關問題