Why ini file is not being saved?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-16 12:02:14

问题


I am trying to save unicode string. There is what I have:

Uses IniFiles;
const
SZ_APP_NAME;

Procedure TForm1.SaveSettings;
var
  _MemIniU: TMemIniFile;
  SettingsPath: string;
begin
  SettingsPath := GetHomePath + PathDelim + SZ_APP_NAME + PathDelim;
  _MemIniU := TMemIniFile.Create(ChangeFileExt(SettingsPath, 'Settings.ini'),
    TEncoding.UTF8);
  try
    if Form1.WindowState <> TWindowState.wsMaximized then
    begin
      _MemIniU.WriteInteger(SZ_APP_NAME, 'WindowLeft', Form1.Left);
      _MemIniU.WriteInteger(SZ_APP_NAME, 'WindowTop', Form1.Top);
      _MemIniU.WriteInteger(SZ_APP_NAME, 'WindowWidth', Form1.Width);
      _MemIniU.WriteInteger(SZ_APP_NAME, 'WindowHeight', Form1.Height);
      _MemIniU.WriteString(SZ_APP_NAME, 'UnicodeText', Edit1.Text);
    end;
  finally
    _MemIniU.Free;
  end;
end;

But ini file is NOT created. What I am doing wrong? RAD Studio XE5.


回答1:


You have to call TMemIniFile.UpdateFile to save the information into the file.

Documentation is very clear to that

TMemIniFile buffers all changes to the INI file. The INI file is read once, when the object is first created. Data from the INI file is stored in nested string lists. Each section in the INI file occupies one element in the top-most string list, and each element in this may itself contain a string list. Each element in each of the contained string list represents a key within the section. After the data is read, any changes to the data are stored in memory. To write the data from memory back to the associated INI file, call the UpdateFile method.

DocWiki TMemIniFile




回答2:


I modified your code. You can see there is "_MemIniU.UpdateFile;" command below.

If you don't use UpdateFile command, any changes you make will not be saved.

Best Regards

Uses IniFiles;
const
SZ_APP_NAME;

Procedure TForm1.SaveSettings;
var
  _MemIniU: TMemIniFile;
  SettingsPath: string;
begin
  SettingsPath := GetHomePath + PathDelim + SZ_APP_NAME + PathDelim;
  _MemIniU := TMemIniFile.Create(ChangeFileExt(SettingsPath, 'Settings.ini'),
    TEncoding.UTF8);
  try
    if Form1.WindowState <> TWindowState.wsMaximized then
    begin
      _MemIniU.WriteInteger(SZ_APP_NAME, 'WindowLeft', Form1.Left);
      _MemIniU.WriteInteger(SZ_APP_NAME, 'WindowTop', Form1.Top);
      _MemIniU.WriteInteger(SZ_APP_NAME, 'WindowWidth', Form1.Width);
      _MemIniU.WriteInteger(SZ_APP_NAME, 'WindowHeight', Form1.Height);
      _MemIniU.WriteString(SZ_APP_NAME, 'UnicodeText', Edit1.Text);
      _MemIniU.UpdateFile;
    end;
  finally
    _MemIniU.Free;
  end;
end;


来源:https://stackoverflow.com/questions/20674809/why-ini-file-is-not-being-saved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!