【问题标题】:Edit .ini file in Delphi to change full paths在 Delphi 中编辑 .ini 文件以更改完整路径
【发布时间】:2022-01-15 17:03:21
【问题描述】:

大家好,我是新手,在 Delphi 中需要一些帮助

我需要帮助 delphi 读取 example.ini 文件并将 path= 更改为当前路径 和 savepath= 到 currentpath\saves

谢谢

【问题讨论】:

  • 您应该首先阅读TIniFile 上的评论。在那里,您可以阅读 Delphi 如何处理 Ini 文件,甚至可以找到可以帮助您入门的示例代码。

标签: delphi path edit filepath ini


【解决方案1】:

这样的事情应该可以满足您的要求(在 Delphi 10.3 CE 中编码):

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.IniFiles;

type
  TForm1 = class(TForm)
  procedure FormCreate(Sender: TObject);
  procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  path: string;
  savepath: string;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  Ini: TIniFile;
begin
  // Extract current directory
  path := ExtractFilePath(ParamStr(0));

  Ini := TIniFile.Create(path + 'example.ini');
  try
    path:= Ini.ReadString('General', 'OpenPath', path);
    savepath:= Ini.ReadString('General', 'SavePath', path + 'saves/');
  finally
    Ini.Free;
  end;
end;

procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
var Ini: TIniFile;
begin
  Ini := TIniFile.Create(path + 'example.ini');
  try
    Ini.WriteString('General', 'OpenPath', path);
    Ini.WriteString('General', 'SavePath', savepath);
  finally
    Ini.Free;
  end;
  CanClose:= true;
end;

end.

【讨论】:

  • 谢谢兄弟,我有 Embarcadero Delphi XE,但是我在 Delphi 7 中的所有项目都在 Delphi XE 上运行,我今晚会试试,如果可行,请告诉你:) 非常感谢
  • 谢谢,但在 Embarcadero Delphi XE 上出现多个错误
  • @johndoe 通常您唯一需要更新的是uses 部分,这在最近的 Delphi 版本中略有不同。我将代码更新到我拥有的最新 Delphi 版本(Delphi 10.3 社区版)
  • 我做了兄弟,非常感谢你的帮助,我可以从 0 开始在 delphi 上完全编写 .ini,这个问题是应用程序每次更改 .ini 上的参数并打开项目都会重置.ini 上的所有配置然后我只需要读取路径和保存路径就可以在那里写入。我在 Borland Delphi 7 之前使用过,但我停止使用 bcuz 我所有的项目(简单的应用程序)都被许多像 troyans 这样的免费抗病毒软件标记,即使我有一个干净的代码,它是一个误报,所以我必须从 Delphi 搬走7到Embarcadero Delphi XE,我编译的所有项目都没有误报。谢谢!!!
猜你喜欢
  • 2023-04-01
  • 2017-12-15
  • 1970-01-01
  • 2016-03-03
  • 2014-07-19
  • 1970-01-01
  • 2011-09-06
  • 2015-08-29
  • 2022-01-11
相关资源
最近更新 更多