Delphi7文件使用

Delphi7文件使用,delphi,delphi-7,Delphi,Delphi 7,我正在尝试创建一个ini文件来保存应用程序配置。 保存部分与编辑框1、2、3等的输入完美配合。以下是ini示例 [1server] SSHHost=ssh.com SSHPort=443 Username=user Password=123 [2server] SSHHost=ssh.com SSHPort=443 Username=user Password=123 [ProxySettings] Proxy=127.0.0.1 Port=8080 Type=http 如何使应用程序在启动时

我正在尝试创建一个ini文件来保存应用程序配置。 保存部分与编辑框1、2、3等的输入完美配合。以下是ini示例

[1server]
SSHHost=ssh.com
SSHPort=443
Username=user
Password=123
[2server]
SSHHost=ssh.com
SSHPort=443
Username=user
Password=123
[ProxySettings]
Proxy=127.0.0.1
Port=8080
Type=http

如何使应用程序在启动时读取保存的ini设置,以及是否可以隐藏或加密用户保存的密码?

只需在主窗体的
FormCreate
事件中读取ini文件即可

procedure TForm1.FormCreate(Sender: TObject);
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(YourIniFileName);
  try
    // The final parameter to all of the `ReadXx` functions is a default
    // value to use if the value doesn't exist.
    Edit1.Text := Ini.ReadString('1server', 'SSHHost', 'No host found');
    Edit2.Text := Ini.ReadString('1server', 'SSHPort', 'No port found');
    // Repeat, using ReadString, ReadInteger, ReadBoolean, etc.
  finally
    Ini.Free;
  end;
end;
需要注意的一点是:
tMiniFile
已知存在写入网络位置的问题,因此如果有可能,您应该改用
tMiniFile
tMiniFile
包装了WinAPI INI支持函数(
ReadPrivateProfileString
和其他函数),而
tMiniFile
完全是用Delphi代码编写的,不会遇到同样的问题。它们是语法兼容的,并且在同一个单元中,因此只需在变量声明和创建
Ini
的行中将
TIniFile
更改为
TMemIniFile

var
  Ini: TMemIniFile;
begin
  Ini := TMemIniFile.Create(YourIniFileName);
  ...
end;

就加密密码而言,您可以使用任何想要的加密算法,只要加密值可以转换为文本表示。(Ini文件不处理二进制值。)算法的选择取决于您试图实现的安全级别。

只需在主窗体的
FormCreate
事件中读取Ini文件即可

procedure TForm1.FormCreate(Sender: TObject);
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(YourIniFileName);
  try
    // The final parameter to all of the `ReadXx` functions is a default
    // value to use if the value doesn't exist.
    Edit1.Text := Ini.ReadString('1server', 'SSHHost', 'No host found');
    Edit2.Text := Ini.ReadString('1server', 'SSHPort', 'No port found');
    // Repeat, using ReadString, ReadInteger, ReadBoolean, etc.
  finally
    Ini.Free;
  end;
end;
需要注意的一点是:
tMiniFile
已知存在写入网络位置的问题,因此如果有可能,您应该改用
tMiniFile
tMiniFile
包装了WinAPI INI支持函数(
ReadPrivateProfileString
和其他函数),而
tMiniFile
完全是用Delphi代码编写的,不会遇到同样的问题。它们是语法兼容的,并且在同一个单元中,因此只需在变量声明和创建
Ini
的行中将
TIniFile
更改为
TMemIniFile

var
  Ini: TMemIniFile;
begin
  Ini := TMemIniFile.Create(YourIniFileName);
  ...
end;

就加密密码而言,您可以使用任何想要的加密算法,只要加密值可以转换为文本表示。(Ini文件不处理二进制值。)算法的选择取决于您试图实现的安全级别。

只需在主窗体的
FormCreate
事件中读取Ini文件即可

procedure TForm1.FormCreate(Sender: TObject);
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(YourIniFileName);
  try
    // The final parameter to all of the `ReadXx` functions is a default
    // value to use if the value doesn't exist.
    Edit1.Text := Ini.ReadString('1server', 'SSHHost', 'No host found');
    Edit2.Text := Ini.ReadString('1server', 'SSHPort', 'No port found');
    // Repeat, using ReadString, ReadInteger, ReadBoolean, etc.
  finally
    Ini.Free;
  end;
end;
需要注意的一点是:
tMiniFile
已知存在写入网络位置的问题,因此如果有可能,您应该改用
tMiniFile
tMiniFile
包装了WinAPI INI支持函数(
ReadPrivateProfileString
和其他函数),而
tMiniFile
完全是用Delphi代码编写的,不会遇到同样的问题。它们是语法兼容的,并且在同一个单元中,因此只需在变量声明和创建
Ini
的行中将
TIniFile
更改为
TMemIniFile

var
  Ini: TMemIniFile;
begin
  Ini := TMemIniFile.Create(YourIniFileName);
  ...
end;

就加密密码而言,您可以使用任何想要的加密算法,只要加密值可以转换为文本表示。(Ini文件不处理二进制值。)算法的选择取决于您试图实现的安全级别。

只需在主窗体的
FormCreate
事件中读取Ini文件即可

procedure TForm1.FormCreate(Sender: TObject);
var
  Ini: TIniFile;
begin
  Ini := TIniFile.Create(YourIniFileName);
  try
    // The final parameter to all of the `ReadXx` functions is a default
    // value to use if the value doesn't exist.
    Edit1.Text := Ini.ReadString('1server', 'SSHHost', 'No host found');
    Edit2.Text := Ini.ReadString('1server', 'SSHPort', 'No port found');
    // Repeat, using ReadString, ReadInteger, ReadBoolean, etc.
  finally
    Ini.Free;
  end;
end;
需要注意的一点是:
tMiniFile
已知存在写入网络位置的问题,因此如果有可能,您应该改用
tMiniFile
tMiniFile
包装了WinAPI INI支持函数(
ReadPrivateProfileString
和其他函数),而
tMiniFile
完全是用Delphi代码编写的,不会遇到同样的问题。它们是语法兼容的,并且在同一个单元中,因此只需在变量声明和创建
Ini
的行中将
TIniFile
更改为
TMemIniFile

var
  Ini: TMemIniFile;
begin
  Ini := TMemIniFile.Create(YourIniFileName);
  ...
end;

就加密密码而言,您可以使用任何想要的加密算法,只要加密值可以转换为文本表示。(Ini文件不处理二进制值。)算法的选择取决于您试图实现的安全级别。

您应该看看其他程序如何处理ssh密码等问题。@KenWhite这是我尝试运行应用程序时读取配置文件的代码行编辑框
过程TForm1.actReloadExecute中没有应用任何内容(发送方:TObject);begin ReadIniFile;end;过程TForm1.ReadIniFile;var MyIni:tinfile;begin MyIni:=tinfile.Create(ChangeFileExt(Application.ExeName,'.ssh');try Edit3.Text:=MyIni.ReadString('1server','SSHHost','');finally myINI.Free;end;end;
如果您觉得需要添加更多详细信息,您的问题是在哪里可以看到它们,代码可以正确格式化并易于阅读。如果您发布的代码没有返回值,则可能是ini文件不在您认为的位置,或者您没有为
SSHHost保存值用于ini文件中的
1server
。您应该看看其他程序是如何处理ssh密码之类的问题的。@KenWhite这是我在尝试运行应用程序时读取配置文件的代码行编辑框
procedure TForm1.actReloadExecute(发件人:TObject);begin ReadIniFile;end;过程TForm1.ReadIniFile;var MyIni:tinfile;begin MyIni:=tinfile.Create(changefilext(Application.ExeName,'.ssh');try Edit3.Text:=MyIni.ReadString('1server','SSHHost','');finally myINI.Free;end;end;
如果您觉得需要添加更多详细信息,您的问题是在哪里可以看到这些信息,在哪里可以正确格式化代码并易于阅读。如果您发布的代码没有返回值,则可能是ini文件不在您认为的位置,或者您没有va