ini文件内容如下
[fgrg]
user=t1
pws=l
[rgfgrg]
user=2y
pws=l
[rfgrgg]
user=3y
pws=l
[rfgrgg]
user=3y
pws=l各个小节名不规则,想要的是将各小节按这个规则重命名user+小节序号 ,如,第一小节命名为[user1],第二小节命名为[user2]...请大家帮帮忙完成以下过程。
function aduser(user:string):Boolean;
 begin
1,将文件中所有小节按规则重新命名,
2,判断user是已经存在,如果user不存在增加一个新小节到最后一行。如果user已经存在返加false
end;

解决方案 »

  1.   

    用一个StringList读整个文件,然后更名就是了.
      

  2.   

    var
      iniFileStrings: TStrings;
      iniFileName,SectionName: String;
      I,Index: Integer;
    begin
      iniFileName := 'c:\test.ini';
      iniFileStrings := TStringList.Create;
      try
        iniFileStrings.LoadFromFile(iniFileName);
        Index := 1;
        for i := 0 to iniFileStrings.Count - 1 do
          begin
            SectionName := iniFileStrings.Strings[i];
            if Length(SectionName)<2 then continue;
            if (SectionName[1] = '[') and (SectionName[Length(SectionName)] = ']') then
              begin
                iniFileStrings.Strings[i] := 'User' + IntToStr(Index);
                Inc(Index);
              end;
          end;
        iniFileStrings.SaveToFile(iniFileName);
      finally
        iniFileStrings.Free;
      end;
    end;
      

  3.   


    procedure TForm1.Button1Click(Sender: TObject);
    const
      fn = 'd:\1.ini';
    var
      A   : TStringList;
      i, j: Integer;
    begin
      A := TStringList.Create;
      try
        A.LoadFromFile(fn);
        j := 1;
        for i := 0 to A.Count - 1 do
        begin
          if Copy(A.Strings[i], 1, 1) = '[' then
          begin
            A.Strings[i] := '[User' + IntToStr(j) + ']';
            inc(j);
          end;
        end;
        A.SaveToFile(fn);
      finally
        A.Free;
      end;
    end;
      

  4.   

    iniFileStrings.Strings[i] := '[User' + IntToStr(Index) + ']'; 
      

  5.   

    好的,重命名部分感谢楼上两个兄弟的指教,但还有一部份希望能补充一下.
    请大家帮帮忙完成以下过程。增加用户名 
    function aduser(user:string,pws:string):Boolean; 
    begin 
     判断user是已经存在,如果user不存在增加一个新小节到最后一行。如果user已经存在返加false 
    end; 
      

  6.   

    借用unsigned的代码,继续做作业function aduser(user:string,pws:string):Boolean; 
    var
      iniFileStrings: TStrings;
      iniFileName,SectionName: String;
      I,Index: Integer;
      NotExist:boolean;
    begin
      iniFileName := 'c:\test.ini';
      iniFileStrings := TStringList.Create;
      try
        NotExist:=True;
        iniFileStrings.LoadFromFile(iniFileName);
        Index := 1;
        for i := 0 to iniFileStrings.Count - 1 do
          begin
            SectionName := iniFileStrings.Strings[i];
            if Length(SectionName)<2 then continue;
            if (SectionName[1] = '[') then Inc(Index);
            if pos('['+user+']',SectionName)>0 NotExist:=False;
          end;
        if NotExist then IniFileStrings.Add('[User' + IntToStr(Index)+']');
        iniFileStrings.SaveToFile(iniFileName);
        Result:=NotExist;
      finally
        iniFileStrings.Free;
      end;
    end;