其他分享
首页 > 其他分享> > Delphi 更改扩展名函数ChangeFileExt

Delphi 更改扩展名函数ChangeFileExt

作者:互联网

function ChangeFileExt(const FileName, Extension: string): string;
var
  I: Integer;
begin
  I := LastDelimiter('.' + PathDelim + DriveDelim,Filename);
  if (I = 0) or (FileName[I] <> '.') then I := MaxInt;
  Result := Copy(FileName, 1, I - 1) + Extension;
end;

用途:当我们想在程序目录建立一个ini配置文件的时候,可快速生成ini路径。

procedure TForm1.FormCreate(Sender: TObject);
var
  ini: TIniFile;
begin
  ini := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
  try
    Self.Left := ini.ReadInteger('Setting', 'Left', 0);
    Self.Top := ini.ReadInteger('Setting', 'Top', 0);
    Self.Width := ini.ReadInteger('Setting', 'Width', Self.Width);
    Self.Height := ini.ReadInteger('Setting', 'Height', Self.Height);
  finally
    ini.Free;
  end;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
  ini: TIniFile;
begin
  ini := TIniFile.Create(ChangeFileExt(Application.ExeName, '.ini'));
  try
    ini.WriteInteger('Setting', 'Left', Self.Left);
    ini.WriteInteger('Setting', 'Top', Self.Top);
    ini.WriteInteger('Setting', 'Width', Self.Width);
    ini.WriteInteger('Setting', 'Height', Self.Height);
  finally
    ini.Free;
  end;
end;

标签:ChangeFileExt,扩展名,Self,Height,Width,Setting,ini,end,Delphi
来源: https://www.cnblogs.com/YXGust/p/16576814.html