First, read the example of the INI file
Procedure TForm1.FormCreate (Sender: TObject); Var MyIni: Tinifile; glAppPath: String ; the begin glAppPath: = ExtractFilePath (Application.ExeName); // get the current path running program . MyIni: = Tinifile the Create (glAppPath + ' myini. INI ' ); Edit1.Text: = MyIni.ReadString ( ' the Setting ' , ' name ' , ' ' ); Edit2.Text: = MyIni.ReadString ( ' the Setting ' , ' address ' ,''); Edit3.Text := MyIni.ReadString('Setting','电话',''); MyIni.Free; end; procedure TForm1.N2Click(Sender: TObject); begin Form2 := TForm2.Create(self); Form2.ShowModal; end; procedure TForm1.Button1Click(Sender: TObject); Var MyIni :Tinifile; glAppPath, path :string; begin glAppPath := ExtractFilePath(Application.ExeName); MyIni := Tinifile.Create(glAppPath + 'myini.ini'); Edit1.Text := MyIni.ReadString('Setting','名称',''); Edit2.Text := MyIni.ReadString('Setting','地址',''); Edit3.Text := MyIni.ReadString('Setting','电话',''); MyIni.Free; Path := ExtractFilePath(Application.ExeName); end; end.
Second, write an example of the ini file
procedure TForm2.Button1Click(Sender: TObject); Var MyIni :Tinifile; glAppPath :string; begin glAppPath := ExtractFilePath(Application.ExeName); MyIni := Tinifile.Create(glAppPath + 'myini.ini'); MyIni.WriteString('Setting','名称',Edit1.text); MyIni.WriteString('Setting','地址',Edit2.text); MyIni.WriteString('Setting','电话',Edit3.text); MyIni.Free; Close; end; procedure TForm2.Button2Click(Sender: TObject); begin close; end; end.
Precautions:
1. To read reference unit Inifiles INI file
2. glAppPath: = ExtractFilePath (Application.ExeName); get the path of the currently running program, remember to set the output path about the program for the job otherwise the program installation path is the path of delphi, setting method: project-> options-> Directories / Coditionals the property is set
Output directory: E: \ prj \ ini file (ie the program save path)
Unit output directory: E: \ prj \ ini file (ie the program save path)
MyIni := Tinifile.Create(glAppPath + 'myini.ini');
3, myinin.ini following structure
[The Setting] (attribute group name)
Name (attribute name) = 123 (value)
Address = 456
telephone = 789
Address = 456
telephone = 789
4.
Can read and write to the .INI file using the Windows API function WritePrivateProfileString and GetPrivateProfileString. In fact, reading and writing .INI file can be fully realized by Delphi's built-in functions. Here are some class attributes and methods associated .INI file when reading and writing.
1, TIniFile subject to Delphi, we define a TIniFile object packaged therein the .INI file, and provides methods designed to read and write operations on the INI file. If a program or a method to use TIniFile class properties, it is necessary to manually add a reference to IniFiles unit uses statement in the program unit. 2, Create method which is used to create a process instance TIniFile type INI file. Method declaration: constructor Create (const FileName: string ); Parameters: filename INI file specified in FileName to be created; Note: Before using TIniFile object, you must first create an instance of an INI file using this method. When FileName can contain the path name, defaults to the Windows directory (usually it is C: \ windows, for Windows NT, it is usually c: \ winnt). Examples of using the Create method to create, after use, call the Free method to free the memory. 3, ReadSection method which reads out the names of all the sub-keys specified segment from the INI file, and stores the specified string Strings parameter list object. Method statement: procedure ReadSection (const Section: string ; Strings: TStrings); Parameters: Section specified segment name to be read section; Strings stored sublist specified key name string; Note: ReadSection method is only read into the specified segment All sub-key name, but not the value read subkey. 4, ReadSections method which reads all the section name from the INI file and stored in the Strings parameter specifies the character list. Method declaration: procedure ReadSections (Strings: TStrings) ; Parameter Description: Strings parameter indicates the storage section name list of strings; Notes: ReadSections method INI file section name in all segments of the read-out, into a designated list of strings this list of strings can use the Items property of a list box directly. 5, ReadSectionValues method which reads in all the sub-keys and key values from the segment specified INI file, and stores the specified parameter list Strings string. Method declaration: procedure ReadSectionValues (const Section: String ; Strings: TStrings); Parameter Description: Section specifies the section to read the section name; a list of strings Strings specified storage section name; NOTE: The difference ReadSectionValues method ReadSection read only the latter method is that the sub-keys, before reading by addition subkey name, also reads the key corresponding to the sub-key. Read key stored subkey name and methods consistent with the list of strings in the document display method, i.e., "Key = Value" form. 6, EraseSection method which deleted a whole section in the INI file specified. Method declaration: procedure EraseSection (const Section: string ); Parameters: Section specifies the section name to be deleted segments; Notes: EraseSection method not only delete the segment name specified segment, while the face and all sub keys and key stage of the deletion . 7, DeleteKey method the method to remove a specified subkey in the specified segment. Method declaration: procedure DeleteKey (const Section, Key : string); Parameters: Section indicated to be a section name deleted subkey data segments; Key indicated to be the key name to delete the sub keys; Notes: DeleteKey method to delete the entire sub-keys (including key names and values), that is, remove the key sub-line is located. 8, ReadBool method which reads a Boolean value specifying the key for the sub-segment. Method statement: function ReadBool (const Section, Key : string; Default: Boolean): Boolean; Parameters: Section name specifying the segment where the segment to be read subkey; Key keys specified to be read-sub-key; Return Value Default parameters specified when the default. NOTE: ReadBool method for reading a Boolean value of a sub-key, when the key is "1", it returns True, the key is "0", return False. . 9, WriteBool method of a method to specify the segment sub-key Writes a Boolean value. Method statement: procedure WriteBool (const Section, Key : string; Value: Boolean); Parameters: Senction specified name to be written to the segment section where the sub-key; Key parameter specifies the name of the sub-bonds value to be written; the Value to be specified Boolean written; Note: WriteBool method for writing a Boolean value subkey, when value is "True", write "1." When Value is "Flase", write "0." If at the time of writing, the specified section or key does not exist, automatically create the segment and key name. 10, ReadInteger method which reads an integer value of a specified period subkey. Method statement: function ReadInteger (const Section, Key : string; Default: longint): longint; Note: This method ReadBool method similar, but different types of variables. 11, WriteInteger method This method refers to the written section of a sub-key place to write an integer value. Method statement: procedure WriteInteger (const Section, Key : string; Value: longint); NOTE: This method WriteBool method similar, but different type of variable ash. 12, ReadString method which reads a string value for a sub-segment specified key. Method statement: function ReadString (const Section, Key : string; Default: string): string; notation; ReadBool method This method is similar to, but different types of variables. 13, WriteString method which is written to the integer value of a sub-section refers to the write key. Method declaration: procedure WriteString (const Section, Key : string; Value: string); Note: This method is WriteBool method similar, but different types of variables. 14, FileName property This property indicates the file name is encapsulated in the object TIniFile INI file. Property declaration: property FileName: string; Note: The FileName property is a read-only attribute runtime. By the side of introduction, we can see a strong Delphi support for INI files is very comprehensive. When we write programs involved in such operations, almost without using the Windows API function |