我编写了一个代码,让我将变量保存到带有自定义文件名的.ini文件中.文件名取决于EditBox1中的文本.没问题.我的问题是如何通过从加载文件窗口( Windows资源管理器)中选择文件来从该自定
DataIni:=TIniFile.Create(getcurrentdir+'\save folder\' + EditBox1.Text +'.ini');
样本保存变量
DataIni.WriteString('INFO','FirstName',EditFirstName.Text); DataIni.WriteString('INFO','MiddleName',EditMiddleName.Text); DataIni.WriteString('INFO','FamilyName',EditFamilyName.Text);
样本加载变量
EditFirstName.Text := DataIni.ReadString('INFO','FirstName',EditFirstName.Text); EditMiddleName.Text := DataIni.ReadString('INFO','MiddleName',EditMiddleName.Text); EditFamilyName.Text := DataIni.ReadString('INFO','FamilyName',EditFamilyName.Text);
因此,例如EditBox1.Text =’MyFile1′,如何通过从其他文件列表中选择MyFile1.ini来加载MyFile1.ini中保存的变量?
您可以使用TOpenDialog获取所需文件的名称,因此您可以调用函数来加载值.uses IniFiles; ...
单击该按钮,调用OpenDialog.Execute以允许选择文件名.
procedure TForm1.Button1Click(Sender: TObject); begin with TOpenDialog.Create(Self) do try Filter := 'Ini Files (*.ini)|*.ini'; if Execute then LoadMyVars(FileName); finally Free; end; end;
此函数将ini文件的值加载到控件.
procedure TForm1.LoadMyVars(const AFileName: string); var DataIni : TIniFile; begin DataIni := TIniFile.Create(AFileName); try EditFirstName.Text := DataIni.ReadString('INFO','FirstName', ''); EditMiddleName.Text := DataIni.ReadString('INFO','MiddleName', ''); EditFamilyName.Text := DataIni.ReadString('INFO','FamilyName', ''); finally FreeAndNil(DataIni); end; end;