AppIni := TIniFile.Create('WIN.INI'); AppIni.ReadSections(ListBox1.Items); AppIni.ReadSection('Ports',Listbox2.Items); AppIni.ReadSectionValues('Ports',ListBox3.Items); iniFile由Section组成,Section下又为:<key>=<value>格式 用ReadSection读key,ReadSectionValues读value,得到的两个列表一一对应 Call ReadSection to retrieve the names of all keys within a specified section of an INI file into a string list object.还可以用function ReadString(const Section, Ident, Default: String): String; override; ident为key名,整个函数返回value,而第三个参数介绍具体看帮助
写的话,把上面函数的read换为write
with TiniFile.create('myini.ini') do begin readsection('setup',mystrlist.Items); Free; end; 其中mystrlist是一个TListBox控件,这样就可以读取=前的内容了
用writestring,下面时标准的帮助,写值时就不用=号 procedure TForm1.Button1Click(Sender: TObject);var MyIniFile: TIniFile; begin MyIniFile := TIniFile.Create('myapp.ini'); Memo1.Clear; MyIniFile.ReadSectionValues('Transfer', Memo1.Lines); if Memo1.Lines.Values['Title1'] <> 'Picture Painter' then MyIniFile.WriteString('Transfer', 'Title1', 'Picture Painter'); MyIniFile.Free; end;procedure TForm1.Button2Click(Sender: TObject);var MyIniFile: TIniFile; begin MyIniFile := TIniFile.Create('myapp.ini'); { if the entry wasn抰 there before, delete it now } if Memo1.Lines.Values['Title1'] = '' then MyIniFile.DeleteKey('Transfer', 'Title1') { otherwise, restore the old value } else MyIniFile.WriteString('Transfer', 'Title1', Memo1.Lines.Values['Title1']); MyIniFile.Free; end;
赫赫,人家是不要=的,其实可以用TStrings来解决! var temp:tstrings; index:integer; .... Temp:=TStringList.Create; Temp.LoadFromFile('YourFileName.Ini'); index:=Temp.IndexOf('Your Ini Section Name'); if index<>=1 then Temp.Insert(index+1,'Your String'); ...... end;
TStringList中怎么控制都很灵活,直接Add就可以加入你的字符串了,TMemIniFile的SetStrings方法直接设置其内容。
试试吧,应该可以的。
AppIni.ReadSections(ListBox1.Items);
AppIni.ReadSection('Ports',Listbox2.Items);
AppIni.ReadSectionValues('Ports',ListBox3.Items);
iniFile由Section组成,Section下又为:<key>=<value>格式
用ReadSection读key,ReadSectionValues读value,得到的两个列表一一对应
Call ReadSection to retrieve the names of all keys within a specified section of an INI file into a string list object.还可以用function ReadString(const Section, Ident, Default: String): String; override;
ident为key名,整个函数返回value,而第三个参数介绍具体看帮助
begin
readsection('setup',mystrlist.Items);
Free;
end;
其中mystrlist是一个TListBox控件,这样就可以读取=前的内容了
比如:
[setup]"adjksjdkj"
dfdsfdsf
dsfdsafds[files]dsfdsf
dskjfkdskjf
可是我 在写入时总是有个犯人的‘=’
请各位详细点
谢谢
procedure TForm1.Button1Click(Sender: TObject);var
MyIniFile: TIniFile;
begin
MyIniFile := TIniFile.Create('myapp.ini');
Memo1.Clear;
MyIniFile.ReadSectionValues('Transfer', Memo1.Lines);
if Memo1.Lines.Values['Title1'] <> 'Picture Painter' then
MyIniFile.WriteString('Transfer', 'Title1', 'Picture Painter');
MyIniFile.Free;
end;procedure TForm1.Button2Click(Sender: TObject);var
MyIniFile: TIniFile;
begin MyIniFile := TIniFile.Create('myapp.ini'); { if the entry wasn抰 there before, delete it now }
if Memo1.Lines.Values['Title1'] = '' then
MyIniFile.DeleteKey('Transfer', 'Title1')
{ otherwise, restore the old value }
else
MyIniFile.WriteString('Transfer', 'Title1', Memo1.Lines.Values['Title1']);
MyIniFile.Free;
end;
;注释
[小节名]
关键字=值
...---- INI文件允许有多个小节,每个小节又允许有多个关键字, “=”后面是该关键字的值。
---- 值的类型有三种:字符串、整型数值和布尔值。其中字符串存贮在INI文件中时没有引号,布尔真值用1表示,布尔假值用0表示。
---- 注释以分号“;”开头。 二、定义
---- 1、在Interface的Uses节增加IniFiles;
---- 2、在Var变量定义部分增加一行:
myinifile:Tinifile;
---- 然后,就可以对变量myinifile进行创建、打开、读取、写入等操作了。 三、打开INI文件
myinifile:=Tinifile.create('program.ini');
--- 上面这一行语句将会为变量myinifile与具体的文件 program.ini建立联系,然后,就可以通过变量myinifile,来读写program.ini文件中的关键字的值了。 ---- 值得注意的是,如果括号中的文件名没有指明路径的话,那么这个Program.ini文件会存储在Windows目录中,把Program.ini文件存储在应用程序当前目录中的方法是:为其指定完整的路径及文件名。下面的两条语句可以完成这个功能:
Filename:=ExtractFilePath(Paramstr(0))+'program.ini';
myinifile:=Tinifile.Create(filename);四、读取关键字的值
--- 针对INI文件支持的字符串、整型数值、布尔值三种数据类型,TINIfiles类提供了三种不同的对象方法来读取INI文件中关键字的值。
--- 假设已定义变量vs、vi、vb分别为string、 integer、boolean类型。
vs:=myinifile.Readstring('小节名','关键字',缺省值);
vi:=myinifile.Readinteger('小节名','关键字',缺省值);
vb:=myinifile.Readbool('小节名','关键字',缺省值);--- 其中缺省值为该INI文件不存在该关键字时返回的缺省值。 五、写入INI文件
---- 同样的,TInifile类也提供了三种不同的对象方法,向INI文件写入字符串、整型数及布尔类型的关键字。
myinifile.writestring('小节名','关键字',变量或字符串值);
myinifile.writeinteger('小节名','关键字',变量或整型数值);
myinifile.writebool('小节名','关键字',变量或True或False);
---- 当这个INI文件不存在时,上面的语句还会自动创建该INI文件。 六、删除关键字
---- 除了可用写入方法增加一个关键字,Tinifile类还提供了一个删除关键字的对象方法:
myinifile.DeleteKey('小节名','关键字');七、小节操作
--- 增加一个小节可用写入的方法来完成,删除一个小节可用下面的对象方法:
myinifile.EraseSection('小节名');--- 另外Tinifile类还提供了三种对象方法来对小节进行操作:
--- myinifile.readsection('小节名',TStrings变量);可将指定小节中的所有关键字名读取至一个字符串列表变量中;
--- myinifile.readsections(TStrings变量);可将INI文件中所有小节名读取至一个字符串列表变量中去。
---- myinifile.readsectionvalues('小节名',TStrings变量);可将INI文件中指定小节的所有行(包括关键字、=、值)读取至一个字符串列表变量中去。 八、释放
在适当的位置用下面的语句释放myinifile:
myinifile.distory;
var
temp:tstrings;
index:integer;
....
Temp:=TStringList.Create;
Temp.LoadFromFile('YourFileName.Ini');
index:=Temp.IndexOf('Your Ini Section Name');
if index<>=1 then
Temp.Insert(index+1,'Your String');
......
end;
问题是 Kingron(戒网中……)大侠的方法
我试用之后我的 文件什么也没有改变,
也就是没有 把我的字符串 写入文件
为什么也 ???我花了100分了!!!