看看下面这一段,包你觉得爽!
procedure InsertLine(FileName: String; LineNO: Integer; IStr: String);
var FL: TStringList;
begin
FL := TStringList.Create;
if (FileName<>'') and (LineNO>0) and (IStr<>'') then
if FL.Count>=LineNO then
try
FL.LoadFromFile(FileName);
FL.Insert(LineNO-1,IStr);
FL.SaveToFile(FileName);
finally
FL.Free;
end
else Application.MessageBox(PChar('原文件少于'+IntToStr(LineNO)+'行!'),
'错误!', MB_ICONERROR+MB_OK);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
InsertLine(OpenDialog1.FileName,5,
'“看看,这就是新插入的行!”');
end;