2013-03-03 102 views
2

我有一個INI文件存儲一些整數的設置。部分名稱爲存儲這樣的:遞增一個INI文件的節號

[ColorScheme_2] 
name=Dark Purple Gradient 
BackgroundColor=224 
BackgroundBottom=2 
BackgroundTop=25 
... 

[ColorScheme_3] 
name=Retro 
BackgroundColor=5 
BackgroundBottom=21 
BackgroundTop=8 
... 

我需要找出一種方法來創建新的部分,即增加從最高的節號配色方案數+1。我有一個列出當前顏色名稱的組合框,所以當用戶保存到INI文件時,現有的方案只會被覆蓋。如何檢查ComboBox文本以查看它是否是現有的部分,如果不是,請使用增加的名稱創建一個新的部分? (即從上面的示例代碼,ColorScheme_2ColorScheme_3已經存在,所以下一個要創建的部分將是ColorScheme_4)。

+4

使用'ReadSections',好了,閱讀的章節。解析它們。增加數量。 – 2013-03-03 18:14:12

+1

您可以使用[ColorScheme \ 2]和ReadSubsections之類的子節點來僅檢索數字部分。但最好的解決方案是使用一個Collection並將序列化和序列化到Ini,並且您根本不必擔心數字 – 2013-03-03 21:41:36

回答

8

您可以通過使用ReadSections方法閱讀所有部分,然後遍歷返回的字符串列表,並分析每個項目在它存儲的最高尋到指標值:

uses 
    IniFiles; 

function GetMaxSectionIndex(const AFileName: string): Integer; 
var 
    S: string; 
    I: Integer; 
    Index: Integer; 
    IniFile: TIniFile; 
    Sections: TStringList; 
const 
    ColorScheme = 'ColorScheme_'; 
begin 
    Result := 0; 
    IniFile := TIniFile.Create(AFileName); 
    try 
    Sections := TStringList.Create; 
    try 
     IniFile.ReadSections(Sections); 
     for I := 0 to Sections.Count - 1 do 
     begin 
     S := Sections[I]; 
     if Pos(ColorScheme, S) = 1 then 
     begin 
      Delete(S, 1, Length(ColorScheme)); 
      if TryStrToInt(S, Index) then 
      if Index > Result then 
       Result := Index; 
     end; 
     end; 
    finally 
     Sections.Free; 
    end; 
    finally 
    IniFile.Free; 
    end; 
end; 

和使用:

procedure TForm1.Button1Click(Sender: TObject); 
begin 
    ShowMessage(IntToStr(GetMaxSectionIndex('d:\Config.ini'))); 
end; 
+0

我會使用'StartsText(ColorScheme,S)'而不是'if Pos(ColorScheme,S)= 1'。 – 2013-03-03 19:55:06

+2

@RemyLebeau,如果我的Delphi版本支持這個功能,我也會。 (注意OP沒有提供任何版本信息)。 – kobik 2013-03-03 20:02:21

+2

'(Ansi)StartsText()'在Delphi 6以上版本中可用。 – 2013-03-03 22:13:33

4

如何檢查ComboBox文本以查看它是否爲現有部分,如果不是,請使用增加的名稱創建一個新的部分?

像這樣:

const 
    cPrefix = 'ColorScheme_'; 
var 
    Ini: TIniFile; 
    Sections: TStringList; 
    SectionName: String; 
    I, Number, MaxNumber: Integer; 
begin 
    Ini := TIniFile.Create('myfile.ini') 
    try 
    SectionName := ComboBox1.Text; 
    Sections := TStringList.Create; 
    try 
     Ini.ReadSections(Sections); 
     Sections.CaseSensitive := False; 
     if Sections.IndexOf(SectionName) = -1 then 
     begin 
     MaxNumber := 0; 
     for I := 0 to Sections.Count-1 do 
     begin 
      if StartsText(cPrefix, Sections[I]) then 
      begin 
      if TryStrToInt(Copy(Sections[I], Length(cPrefix)+1, MaxInt), Number) then 
      begin 
       if Number > MaxNumber then 
       MaxNumber := Number; 
      end; 
      end; 
     end; 
     SectionName := Format('%s%d', [cPrefix, MaxNumber+1]); 
     end; 
    finally 
     Sections.Free; 
    end; 
    // use SectionName as needed... 
    finally 
    Ini.Free; 
    end; 
end; 
+1

我會使用格式('%s%d',[cPrefix,MaxNumber + 1])' – kobik 2013-03-03 20:29:10

+0

是的,這是我的複製/粘貼錯字。 – 2013-03-03 22:15:30

+2

從來沒有聽說過'StartsText',很高興認識。 [+1] – TLama 2013-03-03 23:31:52