(*^_^ 这个分多!有动力!给你写一个函数,愿换哪个的名字,就换哪个的名字,顺序不改变!TINIFile类原本没有这样的方法的!没有加容错处理(比如万一找不到某关键字或小节名),自行加入吧。*)procedure ChangeIdent(IniFile, {INI路径及文件名} Section, {小节名字} oldIdent,{原关键字名字} newIdent {新关键字名字} : string); var Ini : TIniFile; SS : TStringList; I : integer; begin SS := TStringList.Create; SS.NameValueSeparator := '='; Ini := TIniFile.Create(IniFile); Ini.ReadSectionValues(Section, SS); SS[SS.IndexOfName(oldIdent)] := newIdent+'='+SS.Values[oldIdent]; Ini.EraseSection(Section); for I := 0 to SS.Count-1 do Ini.WriteInteger(Section, SS.Names[I], StrtoInt(SS.ValueFromIndex[I])); SS.Free; Ini.Free; end;procedure TForm1.Button1Click(Sender: TObject); {调用方法} begin ChangeIdent('c:\setup.ini', 'Config', 'C', 'C2'); end;
来了function RenameIdent(IniFile, {INI路径及文件名} Section, {小节名字} oldIdent,{原关键字名字} newIdent {新关键字名字} : string; iClass {关键字取值类型} : Byte): boolean; {返回True表示更改成功} {iClass: 0=bool; 1=integer; 2=string; 3=Date; 4=time; 5=Datetime; >5=float} var Ini : TIniFile; SS : TStringList; I : integer; label Err; begin Result := False; if not FileExists(IniFile) then Exit; {如果没有这个INI文件,返回} Ini := TIniFile.Create(IniFile); SS := TStringList.Create; SS.NameValueSeparator := '='; if not Ini.SectionExists(Section) then goto Err;{如果小节不存在,返回} Ini.ReadSectionValues(Section, SS); if SS.IndexOfName(oldIdent) < 0 then goto Err; {如果关键字不存在,返回} SS[SS.IndexOfName(oldIdent)] := newIdent+'='+SS.Values[oldIdent]; try Ini.EraseSection(Section); for I := 0 to SS.Count-1 do case iClass of 0: Ini.WriteBool(Section, SS.Names[I], StrtoBool(SS.ValueFromIndex[I])); 1: Ini.WriteInteger(Section, SS.Names[I], StrtoInt(SS.ValueFromIndex[I])); 2: Ini.WriteString(Section, SS.Names[I], SS.ValueFromIndex[I]); 3: Ini.WriteDate(Section, SS.Names[I], StrtoDate(SS.ValueFromIndex[I])); 4: Ini.WriteTime(Section, SS.Names[I], StrtoTime(SS.ValueFromIndex[I])); 5: Ini.WriteDateTime(Section, SS.Names[I], StrtoDatetime(SS.ValueFromIndex[I])); else Ini.WriteFloat(Section, SS.Names[I], StrtoFloat(SS.ValueFromIndex[I])); end; Result := True; except ; end; Err: SS.Free; Ini.Free; end;procedure TForm1.Button1Click(Sender: TObject); {调用方法} begin if RenameIdent('c:\setup.ini', 'Config', 'C', 'C2', 1) then Showmessage('修改成功!'); end;
procedure ChangeIdent(IniFile, {INI路径及文件名} oldIdent,{原关键字名字} newIdent {新关键字名字} : string); var ss:TStringList; s:string; i:integer; begin ss:=TStringList.Create; try ss.LoadFromFile(inifile); for i:=0 to ss.Count-1 do begin s:=copy(ss[i],1,length(oldIdent)); if s=oldIdent then ss[i]:=newIdent+copy(ss[i],length(oldIdent)+1,length(ss[i])-length(oldIdent)); end; ss.SaveToFile(inifile); finally ss.Free; end; end;
procedure ChangeIdent(IniFile, {INI路径及文件名} oldIdent,{原关键字名字} newIdent {新关键字名字} : string); var ss:TStringList; s:string; i,poseq:integer; begin ss:=TStringList.Create; try ss.LoadFromFile(inifile); for i:=0 to ss.Count-1 do begin poseq:=pos('=',ss[i]); if poseq==length(oldIdent)+1 then begin s:=copy(ss[i],1,poseq-1); if s=oldIdent then begin s:=copy(ss[i],poseq,length(ss[i])-pos+1); ss[i]:=newIdent+s; Break; end; end; end; ss.SaveToFile(inifile); finally ss.Free; end; end;
myinifile.DeleteKey('Config','d');
myinifile.DeleteKey('Config','e');
iniInfo.WriteString('Config','c2','3');
iniInfo.WriteString('Config','d','4');
iniInfo.WriteString('Config','e','5');就这样就行了!!!从来没有赚到分的 这下子应该可以赚到了哇
Section, {小节名字}
oldIdent,{原关键字名字}
newIdent {新关键字名字}
: string);
var
Ini : TIniFile;
SS : TStringList;
I : integer;
begin
SS := TStringList.Create;
SS.NameValueSeparator := '=';
Ini := TIniFile.Create(IniFile);
Ini.ReadSectionValues(Section, SS);
SS[SS.IndexOfName(oldIdent)] := newIdent+'='+SS.Values[oldIdent];
Ini.EraseSection(Section);
for I := 0 to SS.Count-1 do
Ini.WriteInteger(Section, SS.Names[I], StrtoInt(SS.ValueFromIndex[I]));
SS.Free;
Ini.Free;
end;procedure TForm1.Button1Click(Sender: TObject); {调用方法}
begin
ChangeIdent('c:\setup.ini', 'Config', 'C', 'C2');
end;
如果要适应所有类型,可以再加一个参数。
不过现在没有时间帮你改了。
今天有时间一定奉上完全可以扩充成为TIniFile类的一个方法的完整函数!
谁让这贴分多呢!呵呵!写这样一个函数是值得的,因为它通用性强!^_^
Section, {小节名字}
oldIdent,{原关键字名字}
newIdent {新关键字名字}
: string;
iClass {关键字取值类型}
: Byte): boolean; {返回True表示更改成功}
{iClass: 0=bool; 1=integer; 2=string; 3=Date; 4=time;
5=Datetime; >5=float}
var
Ini : TIniFile;
SS : TStringList;
I : integer;
label Err;
begin
Result := False;
if not FileExists(IniFile) then Exit; {如果没有这个INI文件,返回}
Ini := TIniFile.Create(IniFile);
SS := TStringList.Create;
SS.NameValueSeparator := '=';
if not Ini.SectionExists(Section) then goto Err;{如果小节不存在,返回}
Ini.ReadSectionValues(Section, SS);
if SS.IndexOfName(oldIdent) < 0 then goto Err; {如果关键字不存在,返回}
SS[SS.IndexOfName(oldIdent)] := newIdent+'='+SS.Values[oldIdent];
try
Ini.EraseSection(Section);
for I := 0 to SS.Count-1 do
case iClass of
0: Ini.WriteBool(Section, SS.Names[I], StrtoBool(SS.ValueFromIndex[I]));
1: Ini.WriteInteger(Section, SS.Names[I], StrtoInt(SS.ValueFromIndex[I]));
2: Ini.WriteString(Section, SS.Names[I], SS.ValueFromIndex[I]);
3: Ini.WriteDate(Section, SS.Names[I], StrtoDate(SS.ValueFromIndex[I]));
4: Ini.WriteTime(Section, SS.Names[I], StrtoTime(SS.ValueFromIndex[I]));
5: Ini.WriteDateTime(Section, SS.Names[I], StrtoDatetime(SS.ValueFromIndex[I]));
else Ini.WriteFloat(Section, SS.Names[I], StrtoFloat(SS.ValueFromIndex[I]));
end;
Result := True;
except
;
end;
Err:
SS.Free;
Ini.Free;
end;procedure TForm1.Button1Click(Sender: TObject); {调用方法}
begin
if RenameIdent('c:\setup.ini', 'Config', 'C', 'C2', 1) then
Showmessage('修改成功!');
end;
oldIdent,{原关键字名字}
newIdent {新关键字名字}
: string);
var
ss:TStringList;
s:string;
i:integer;
begin
ss:=TStringList.Create;
try
ss.LoadFromFile(inifile);
for i:=0 to ss.Count-1 do
begin
s:=copy(ss[i],1,length(oldIdent));
if s=oldIdent then
ss[i]:=newIdent+copy(ss[i],length(oldIdent)+1,length(ss[i])-length(oldIdent));
end;
ss.SaveToFile(inifile);
finally
ss.Free;
end;
end;
oldIdent,{原关键字名字}
newIdent {新关键字名字}
: string);
var
ss:TStringList;
s:string;
i,poseq:integer;
begin
ss:=TStringList.Create;
try
ss.LoadFromFile(inifile);
for i:=0 to ss.Count-1 do
begin
poseq:=pos('=',ss[i]);
if poseq==length(oldIdent)+1 then
begin
s:=copy(ss[i],1,poseq-1);
if s=oldIdent then
begin
s:=copy(ss[i],poseq,length(ss[i])-pos+1);
ss[i]:=newIdent+s;
Break;
end;
end;
end;
ss.SaveToFile(inifile);
finally
ss.Free;
end;
end;
代码上面都给了,不再赘述了
给几个思路吧1、使用TIniFile类
2、使用VCL中其它文本操作类,如TStings等
3、直接作为文本文件处理