procedure TForm2.SpeedButton1Click(Sender: TObject);
var tmplist:TStringList;
tmpstream:TFileStream;
begin
tmplist:=TStringList.Create ;
if OpenDialog1.Execute then
begin
tmpstream:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead);
tmpstream.Position:=0;
tmplist.LoadFromStream(tmpstream);//这里是读取
showmessage(inttostr(tmplist.Count ));
end;
tmplist.Free ;
tmpstream.Free ;end;
//tmplist.SaveFromStream(tmpstream);//
var tmplist:TStringList;
tmpstream:TFileStream;
begin
tmplist:=TStringList.Create ;
if OpenDialog1.Execute then
begin
tmpstream:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead);
tmpstream.Position:=0;
tmplist.LoadFromStream(tmpstream);//这里是读取
showmessage(inttostr(tmplist.Count ));
end;
tmplist.Free ;
tmpstream.Free ;end;
//tmplist.SaveFromStream(tmpstream);//
procedure TForm1.Button1Click(Sender: TObject);
var
f : TIniFile;
i : integer;
stSection : TStringList ;
begin
f := TiniFile.create('ini文件名');
stSection := TStringList.Create ;
f.ReadSection('小节的名字',stSection);
for i := 0 to stSection.Count -1 do//删除
f.DeleteKey('小节的名字',stSection[i]); for i := 0 to ListBox1.Count -1 do//写入
f.WriteString('小节的名字',intToStr(i+1),ListBox1.Items[i]);
stSection.Free;
end;
需要引用inifiles单元。
for i := 0 to count-1 do
begin
inifile.WriteString('ABC','a'+ IntToStr(I+1),values[i]);
end;如果不知道count
inifile := TINIFile.Create('inifile.ini');
for i := 0 to length(values)-1 do
begin
inifile.WriteString('ABC','a'+ IntToStr(I+1),values[i]);
end;
//其中values是你要保存的数据数组
myCIniFile = class
public
FileName: string;
constructor Create(sFileName: string);
destructor Free;
procedure DeleteKey(sSection,sKey:string);
procedure EraseSection(sSection:string);
function ReadBool(sSection,sKey:string; bDefault:Boolean): Boolean;
function ReadInteger(sSection,sKey:string; iDefault:LongInt): LongInt;
function ReadString(sSection,sKey:string; sDefault:string): string;
procedure WriteBool(sSection,sKey:string; bValue:Boolean);
procedure WriteInteger(sSection,sKey:string; iValue:LongInt);
procedure WriteString(sSection,sKey:string; sValue:string);
end;implementationuses Windows, SysUtils;constructor myCIniFile.Create(sFileName: string);
begin
FileName:= sFileName
end;destructor myCIniFile.Free;
begin
end;procedure myCIniFile.DeleteKey;
begin
WritePrivateProfileString(PChar(sSection), PChar(sKey), nil, PChar(FileName))
end;procedure myCIniFile.EraseSection(sSection:string);
begin
WritePrivateProfileString(PChar(sSection), nil, nil, PChar(FileName))
end;function myCIniFile.ReadBool(sSection,sKey:string; bDefault:Boolean): Boolean;
begin
Result:= bDefault;
Result:= GetPrivateProfileInt(PChar(sSection), PChar(sKey), Ord(bDefault), PChar(FileName))<>0;
end;function myCIniFile.ReadInteger(sSection,sKey:string; iDefault:LongInt): LongInt;
begin
Result:= iDefault;
Result:= GetPrivateProfileInt(PChar(sSection), PChar(sKey), iDefault, PChar(FileName))
end;function myCIniFile.ReadString(sSection,sKey:string; sDefault:string): string;
var
Buffer: array[0..2047] of Char;
begin
Result:= sDefault;
SetString(Result, Buffer, GetPrivateProfileString(PChar(sSection), PChar(sKey), PChar(sDefault), Buffer, SizeOf(Buffer), PChar(FileName)))
end;procedure myCIniFile.WriteBool(sSection,sKey:string; bValue:Boolean);
begin
if bValue then
WritePrivateProfileString(PChar(sSection), PChar(sKey), 'True', PChar(FileName))
else
WritePrivateProfileString(PChar(sSection), PChar(sKey), 'False', PChar(FileName))
end;procedure myCIniFile.WriteInteger(sSection,sKey:string; iValue:LongInt);
begin
WritePrivateProfileString(PChar(sSection), PChar(sKey), PChar(IntToStr(iValue)), PChar(FileName))
end;procedure myCIniFile.WriteString(sSection,sKey:string; sValue:string);
begin
WritePrivateProfileString(PChar(sSection), PChar(sKey), PChar(sValue), PChar(FileName))
end;procedure myCIniFile.Free;
begin
end;end.