如何在Delphi中编辑INI文件

INI files are text-based files used for storing an application's configuration data.

INI文件是基于文本的文件,用于存储应用程序的配置数据。

Even though Windows recommends using the Windows Registry to store application-specific configuration data, in many cases, you'll find that INI files provide a quicker way for the program to access its settings. Windows itself even uses INI files; desktop.ini and boot.ini being just two examples.

尽管Windows建议使用Windows注册表来存储特定于应用程序的配置数据,但在许多情况下,您会发现INI文件为程序访问其设置提供了更快的方法。 Windows本身甚至使用INI文件。 desktop.iniboot.ini只是两个示例。

One simple use of INI files as a status saving mechanism would be to save the size and location of a form if you want a form to reappear at its previous position. Instead of searching through a whole database of information to find the size or location, an INI file is used instead.

INI文件作为状态保存机制的一种简单用法是,如果您希望表单重新出现在其先前位置,则可以保存表单的大小和位置。 与其搜索整个信息数据库而不是大小或位置,而是使用INI文件。

INI文件格式 ( The INI File Format )

Initialization or Configuration Settings file (.INI) is a text file with a 64 KB limit divided into sections, each containing zero or more keys. Each key contains zero or more values.

初始化或配置设置文件(.INI)是一个文本文件,其限制为64 KB,分为几部分,每个部分包含零个或多个键。 每个键包含零个或多个值。

Here's an example:

这是一个例子:

 [SectionName]
keyname1=value
;comment
keyname2=value

Section names are enclosed in square brackets and must begin at the beginning of a line. Section and key names are case-insensitive (the case doesn't matter), and cannot contain spacing characters. The key name is followed by an equal sign ("="), optionally surrounded by spacing characters, which are ignored.

节名称括在方括号中,并且必须从行的开头开始。 段名和键名不区分大小写(大小写无关),并且不能包含空格字符。 键名后跟一个等号(“ =”),并可选地由空格字符包围,这些字符将被忽略。

If the same section appears more than once in the same file, or if the same key appears more than once in the same section, then the last occurrence prevails.

如果同一节在同一文件中出现多次,或者同一键在同一节中出现多次,则以最后一次出现为准。

A key can contain string, integer, or boolean value.​

键可以包含字符串 ,整数或布尔

Delphi IDE uses the INI file format in many cases. For example, .DSK files (desktop settings) utilize the INI format.

在许多情况下, Delphi IDE使用INI文件格式。 例如, .DSK文件 (桌面设置)使用INI格式。

TIniFile类 ( TIniFile Class )

Delphi provides the TIniFile class, declared in the inifiles.pas unit, with methods to store and retrieve values from INI files.

Delphi为在inifiles.pas单元中声明的TIniFile类提供了用于存储和检索INI文件中的值的方法。

Prior to working with the TIniFile methods, you need to create an instance of the class:

在使用TIniFile方法之前,您需要创建该类的实例:

 uses inifiles;
...var
  IniFile : TIniFile;begin
  IniFile := TIniFile.Create('myapp.ini') ;

The above code creates an IniFile object and assigns 'myapp.ini' to the only property of the class — the FileName property used to specify the name of the INI file you are to use.

上面的代码创建一个IniFile对象,并将“ myapp.ini”分配给该类的唯一属性-FileName 属性 - 用于指定您要使用的INI文件的名称。

The code as written above looks for the myapp.ini file in the \Windows directory. A better way to store application data is in the application's folder - just specify the full pathname of the file for the Create method:

上面编写的代码在\ Windows目录中查找myapp.ini文件。 存储应用程序数据的一种更好的方法是在应用程序的文件夹中-只需为Create方法指定文件的完整路径名即可:

 // place the INI in the application folder,
// let it have the application name
// and 'ini' for extension:

iniFile := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')) ;

从INI读取 ( Reading From INI )

The TIniFile class has several "read" methods. The ReadString reads a string value from a key, ReadInteger. ReadFloat and similar are used to read a number from a key. All "read" methods have a default value that can be used if the entry does not exist.

TIniFile类具有几种“读取”方法。 ReadString从键ReadInteger读取字符串值 。 ReadFloat和类似的控件用于从键读取数字。 如果条目不存在,则所有“读取”方法都有默认值。

For example, the ReadString is declared as:

例如,ReadString声明为:

function ReadString(const Section, Ident, Default: String): String; override;

写入INI ( Write to INI )

The TIniFile has a corresponding "write" method for each "read" method. They are WriteString, WriteBool, WriteInteger, etc.

TIniFile对于每个“读取”方法都有一个对应的“写入”方法。 它们是WriteString,WriteBool,WriteInteger等。

For example, if we want a program to remember the name of the last person who used it, when it was, and what the main form coordinates were, we might establish a section called Users, a keyword called Last, Date to track the information, and a section called Placement with keys TopLeftWidth, and Height.

例如,如果我们想让一个程序记住使用它的最后一个人的名字,使用的时间以及主要表单的坐标,我们可以建立一个名为Users的部分,一个名为Last的关键字, Date来跟踪信息。 ,以及一个名为Placement的部分,其中有按键TopLeftWidthHeight

 project1.ini
 [User]
 Last=Zarko Gajic
 Date=01/29/2009
 [Placement]
 Top=20
 Left=35
 Width=500
 Height=340

Note that the key named Last holds a string value, Date holds a TDateTime value, and all keys in the Placement section hold an integer value.

请注意,名为Last的键包含一个字符串值, Date包含TDateTime值,并且Placement部分中的所有键均包含一个整数值。

The OnCreate event of the main form is the perfect place to store the code needed to access the values in the application's initialization file:

主窗体的OnCreate事件是存储访问应用程序初始化文件中的值所需的代码的理想场所:

 procedure TMainForm.FormCreate(Sender: TObject) ;var
  appINI : TIniFile;
  LastUser : string;
  LastDate : TDateTime;begin
  appINI := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')) ;try//if no last user return an empty string
    LastUser := appINI.ReadString('User','Last','') ;//if no last date return todays date
    LastDate := appINI.ReadDate('User', 'Date', Date) ;//show the message
    ShowMessage('This program was previously used by ' + LastUser + ' on ' + DateToStr(LastDate));
    Top := appINI.ReadInteger('Placement','Top', Top) ;
    Left := appINI.ReadInteger('Placement','Left', Left);
    Width := appINI.ReadInteger('Placement','Width', Width);
    Height := appINI.ReadInteger('Placement','Height', Height);finally
    appINI.Free;end;end;

The main form's OnClose event is ideal for the Save INI part of the project.

主窗体的OnClose事件非常适合项目的Save INI部分。

 procedure TMainForm.FormClose(Sender: TObject; var Action: TCloseAction) ;var
  appINI : TIniFile;begin
  appINI := TIniFile.Create(ChangeFileExt(Application.ExeName,'.ini')) ;try
    appINI.WriteString('User','Last','Zarko Gajic') ;
    appINI.WriteDate('User', 'Date', Date) ;with appINI, MainForm dobegin
      WriteInteger('Placement','Top', Top) ;
      WriteInteger('Placement','Left', Left) ;
      WriteInteger('Placement','Width', Width) ;
      WriteInteger('Placement','Height', Height) ;end;finally
    appIni.Free;end;end;

INI节 ( INI Sections )

The EraseSection erases an entire section of an INI file. ReadSection and ReadSections fill a TStringList object with the names of all sections (and key names) in the INI file.

EraseSection擦除INI文件的整个部分。 ReadSectionReadSections用INI文件中所有节的名称(和键名)填充TStringList对象。

INI的局限性和不足 ( INI Limitations & Downsides )

The TIniFile class uses the Windows API which imposes a limit of 64 KB on INI files. If you need to store more than 64 KB of data, you should use the TMemIniFile.

TIniFile类使用Windows API ,这对INI文件施加了64 KB的限制。 如果您需要存储64 KB以上的数据,则应使用TMemIniFile。

Another problem might arise if you have a section with more than 8 K value. One way to solve the problem is to write your own version of the ReadSection method.

如果您的节的值大于8 K,则可能会出现另一个问题。 解决问题的一种方法是编写自己的ReadSection方法版本。

翻译自: https://www.thoughtco.com/manipulate-ini-files-from-delphi-1058227

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值