问题
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