【问题标题】:Incrementing an INI file's section number增加 INI 文件的节号
【发布时间】:2013-02-17 19:05:05
【问题描述】:

我有一个 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)。

【问题讨论】:

  • 使用ReadSections 来阅读这些部分。解析它们。增加数字。
  • 您可以使用 [ColorScheme\2] 和 ReadSubsections 等 SubSection 来仅检索数字部分。但最好的解决方案是使用集合并将集合和项目反序列化到 Ini,您根本不必担心数字

标签: delphi ini


【解决方案1】:

您可以使用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;

【讨论】:

  • @RemyLebeau,如果我的 Delphi 版本支持此功能,我也会。 (请注意,OP 没有提供任何版本信息)。
  • (Ansi)StartsText() 在 Delphi 6 以上版本中可用。
  • @RemyLebeau,是的,D6/7 有 AnsiStartsText。但我尽量避免Ansi 显式字符串函数。
  • @kobik:为什么要避开它们? Ansi...() 函数在 Ansi 版本的 Delphi 中是 Ansi,在 Unicode 版本的 Delphi 中是 Unicode(尽管它们的名字)。
  • @Remy,避免使用它们,因为在我的非 Unicode Delphi 中我想支持 Unicode 以及更高版本的 Unicode Delphi,而 Pos 支持 Unicode。跨度>
【解决方案2】:

如何检查 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;

【讨论】:

  • 我会使用Format('%s%d', [cPrefix, MaxNumber+1])
  • 是的,这是我复制/粘贴的错字。
  • 从未听说过StartsText,很高兴认识。 [+1]
  • @TLama:StrUtils 单元中有很多有用的字符串函数。
猜你喜欢
  • 2012-06-18
  • 2014-01-29
  • 1970-01-01
  • 2015-10-12
  • 2015-02-17
  • 2016-01-09
  • 2012-10-09
  • 2021-05-14
  • 1970-01-01
相关资源
最近更新 更多