【问题标题】:How to save to an INI file delphidelphi如何保存到INI文件
【发布时间】:2017-08-08 19:36:16
【问题描述】:

我有一些按钮,点击后将其标题存储在 TStringList 中

ChairList : TStringList;

procedure TForm1.FormCreate(Sender: TObject);
begin
ChairList := TStringList.Create;
end;

一个按钮的例子是:

procedure TForm1.Table17Click(Sender: TObject);
begin
Label1.Visible := false;
if (BottomPanel.Visible = false) then
begin
  Label1.Visible := true;

  LastClicked := 'Table 17';
  ChairList.Add(LastClicked);

但是当我读取文件时,我没有得到任何结果。我使用ShowMessage 对其进行了测试,只是为了看看是否会读取任何内容,我只是得到一个空白ShowMessage

以下代码是保存过程:

Procedure TForm1.SaveToFile(Const Filename : String);
Var
INI : TMemIniFile;

Procedure SaveReserve();
var
section : String;

Begin
  Section := 'Table';
  ini.writeString(Section, 'LastClickedID', LastClicked);
End;

begin
Ini := Tmeminifile.Create(filename);
ini.Clear;
try
SaveReserve();
Ini.UpdateFile;
finally
Ini.Free;
end;
end;

那么接下来的Procedure就是加载文件了:

Procedure TForm1.LoadFile(const Filename : String);
var
INI : TMemInifile;
Sections : TStringList;
i : Integer;
LastClickedID : String;
Procedure LoadChair(Const Section: String);
Begin
  LastClickedID := INI.ReadString(Section, 'LastClickedID', LastClicked)
End;

Begin
ChairList.Clear;

INI := TMEMINIFILE.Create(Filename);
 Try
 Sections := TStringList.Create;
 Try
   Ini.ReadSections(Sections);
   for i := 0 to (Sections.Count - 1) do
   Begin
     if startstext('LastClickedID', Sections[I]) then
      begin
        LastClickedID := (copy(Sections[I], 12, MaxInt));

      end;
   End;
 Finally
  Sections.Free;
 End;
 Finally
 INI.Free;
end;
ShowTable(LastClickedID);
end;

ShowTable(LastClickedID); is just the part where i test it

我怎样才能让它保存标题,然后当我加载它时,它会显示保存的任何表格标题? 我只需要它来保存所选对象的标题。当用户选择一个按钮时,它会将标题添加到字符串列表中,然后将其读取到 ini 文件中

【问题讨论】:

    标签: file delphi save ini


    【解决方案1】:

    试试这个代码! 写入 ini 的过程:

    procedure write_ini;
    var
      ini:TIniFile;
    begin
      ini:=TIniFile.Create('MainForm.ini');
      ini.WriteInteger('FORM', 'Top', MainForm.Top);
      ini.WriteInteger('FORM', 'Left', MainForm.Left);
      ini.WriteInteger('FORM', 'Width', MainForm.Width);
      ini.WriteInteger('FORM', 'Height', MainForm.Height);
      ini.WriteString('USER', 'Name', MainForm.userName.Text);
      ini.WriteInteger('USER','Pol', MainForm.Combo.ItemIndex);
      ini.Free;
    end;
    

    读取 ini 的过程:

     procedure read_ini;
        var
         ini:TIniFile;
        begin
          ini:=TiniFile.Create('MainForm.ini');
          MainForm.Top:=ini.ReadInteger('FORM', 'Top', 0);
          MainForm.Left:=ini.ReadInteger('FORM', 'Left', 0);
          MainForm.Width:=ini.ReadInteger('FORM', 'Width', 226);
          MainForm.Height:=ini.ReadInteger('FORM', 'Height', 123);
          MainForm.userName.Text:=ini.ReadString('USER', 'Name', 'Anonim');
          MainForm.Combo.ItemIndex:=ini.ReadInteger('USER', 'Pol', 1);
          ini.Free;
        end;
    

    【讨论】:

    • 我只需要它来保存所选对象的标题。当用户选择一个按钮时,它会将标题添加到字符串列表中,然后将其读取到 ini 文件中
    • procedure TForm1.Button4Click(Sender: TObject);开始 //保存到ini文件 ini.WriteString('Caption', 'Caption', MainForm.Caption);结束;
    • @VladIvchenko 它需要 MainForm 部分吗?我在我的程序中使用 INI 文件,我有以下内容: Ini.WriteString(Section, 'Caption', Table.Caption);但这仅有效,因为我将对象保存在对象列表而不是字符串列表中。因此,我没有将标题专门存储在 tstringlist 中,而是将所有对象数据存储在 tobjectlist 中,然后当我想保存到 ini 文件时,我只选择某些属性
    • 您应该真的try .. finally 对象的生命周期内使用try .. finally 块。特别是对于像这样的对象,它们的构造函数和析构函数可能会失败。
    【解决方案2】:

    我建议您阅读此文档和示例to use TMemIniFile and TIniFile。应该会有所帮助...

    这是我的代码:

    Procedure TForm1.SaveToFile(Const Filename : String);
    Var
      Ini : TIniFile;    
    begin
      Ini := TIniFile.Create(filename);      
      try
        Ini.WriteString('Table', 'LastClickedID', LastClicked);
      finally
        Ini.Free;
      end;
    end;
    
    Procedure TForm1.LoadFile(const Filename : String);
    var
      Ini : TInifile;
      LastClickedID : String;    
    begin    
      Ini := TIniFile.Create(Filename);
      try
        LastClickedID := Ini.ReadString('Table', 'LastClickedID', '');
      finally
        Ini.Free;
      end;
      ShowTable(LastClickedID);
    end;
    

    【讨论】:

      【解决方案3】:

      您可以创建一个通用的ini文件实用程序对象,它可以在预定义的键下读/写字典对象收集的属性。这是一个灵活、可重复使用的解决方案。 您可以将单击的控件标题收集到字典中,并在过程结束时保存列表。您应该使用字典,因为 ini 文件操作需要键/值对。

      uses
          Generics.Collections
        , System.IniFiles
        ;
      
      TIniFileUtility = class
        public
          class read( ini_ : TIniFile; keyName_ : string; props_ : TDictionary<string,string> );
          class write( ini_ : TIniFile; keyName_ : string; props_ : TDictionary<string,string> );
      end;
      
      
      
      class procedure TIniFileUtility.write( ini_ : TIniFile; keyName_ : string; props_ : TDictionary<string,string>);
      var
        i : integer;
        key, value : string;
      begin
        //... some input parameter checking
          for key in props_.keys do
          begin
            value := props_.Items[key];
            ini_.writeString( keyName_, key, value );
          end;
      end;
      
      procedure caller(Sender: TObject);
      var
        ini : TIniFile;
        props : TDictionary<string,string>;
      begin
        ini := TIniFile.create( 'c:\temp\defaults.ini' );
        try
          props := TDictionary<string,string>.Create;
          try
            props.Add( 'key1', 'stringValue' );
            props.Add( 'key2', intToStr( 2 ) );
            TIniFileUtility.write( ini, '\x\y\z\', props );
          finally
            props.Free;
            props := NIL;
          end;
        finally
          ini.Free;
          ini := NIL;
        end;
      end;
      

      【讨论】:

        【解决方案4】:

        您的代码无法重新加载该值,因为您试图从 INI 文件的错误区域读取它。

        您正在将值写入'Table' 部分的'LastClickedID' 条目,这会产生此INI 数据:

        [Table]
        LastClickedID=Value
        

        但是您尝试从以 'LastClickedID' 前缀开头的任何部分的 'LastClickedID' 条目中读取值,而不是从您之前写入的 'Table' 部分中读取值。这只有在 INI 数据看起来更像这样的情况下才有效:

        [LastClickedID]
        LastClickedID=Value
        

        由于 INI 中不存在该部分,这就是为什么您的 LastClickedID 变量始终为空白(如果 LastClicked 开头也是空白)。

        在此示例中,您无需使用 Ini.ReadSections()。只需使用 Ini.ReadString() 本身和正确的 'Table' 部分名称,例如:

        procedure TForm1.LoadFile(const Filename : String);
        var
          Ini : TMemIniFile;
          LastClickedID: string;
        
          procedure LoadReserve;
          var
            section : String;
          begin
            Section := 'Table';
            LastClickedID := Ini.ReadString(Section, 'LastClickedID', LastClicked);
          end;
        
        begin
          Ini := TMemIniFile.Create(Filename);
          try
            LoadReserve;
          finally
            Ini.Free;
          end;
        
          ShowTable(LastClickedID);
        end;
        

        一般而言,您应该镜像您的 SaveToFile() 代码正在执行的任何操作。

        【讨论】:

          猜你喜欢
          • 2015-09-14
          • 2016-01-25
          • 2023-03-19
          • 1970-01-01
          • 1970-01-01
          • 2019-05-16
          • 2011-08-06
          • 2011-01-06
          相关资源
          最近更新 更多