Ini 文件:DeleteKey 留下空白部分
当 ini 文件包含只有一个键的部分(例如 中的 MySection1
[MySection1]
MyKey1=MyValue1
[MySection2]
...
)时,调用该键的 DeleteKey
会留下一个空部分:
[MySection1]
[MySection2]
...
我希望该空部分是也去掉了。当然,我可以
if not ini.SectionExists('MySection1') then
ini.EraseSection('MySection1');
在每次调用 DeleteKey
后调用类似的方法(或者重写 TExtIniFile.DeleteKey
来执行此操作),但我希望有一种自动方法可以使 Windows 或VCL 就是这么做的。你知道吗?
更新:我正在使用 TIniFile 后代,它仅添加一些额外的 Read*/Write* 方法。
更新: 我的测试例程:
procedure TForm1.Button1Click(Sender: TObject);
var
ini: TMyIniFile;
begin
ini := TMyIniFile.Create(cIniFileName);
try
ini.WriteString('MySection1', 'MyKey1', 'MyValue1');
ini.DeleteKey('MySection1', 'MyKey1');
finally
ini.Free;
end;
Show;
end;
procedure TForm1.Show;
begin
if FileExists(cIniFileName) then
Memo1.Lines.LoadFromFile(cIniFileName)
else
Memo1.Lines.Clear;
end;
TMyIniFile
可以是 TIniFile
的别名,或者是 TMemIniFile
的后代,并调用析构函数更新文件
。
When an ini file contains a section with only one key (like MySection1
in
[MySection1]
MyKey1=MyValue1
[MySection2]
...
) calling DeleteKey
for that key leaves an empty section:
[MySection1]
[MySection2]
...
I'd prefer if that empty section would be removed too. Of course I can call something like
if not ini.SectionExists('MySection1') then
ini.EraseSection('MySection1');
after every call to DeleteKey
(or make an overridden TExtIniFile.DeleteKey
do that) but I hope there is an automatic way to make Windows or the VCL do that. Do you know any?
Update: I'm using a TIniFile descendant that solely adds some additional Read*/Write* methods.
Update: My test routine:
procedure TForm1.Button1Click(Sender: TObject);
var
ini: TMyIniFile;
begin
ini := TMyIniFile.Create(cIniFileName);
try
ini.WriteString('MySection1', 'MyKey1', 'MyValue1');
ini.DeleteKey('MySection1', 'MyKey1');
finally
ini.Free;
end;
Show;
end;
procedure TForm1.Show;
begin
if FileExists(cIniFileName) then
Memo1.Lines.LoadFromFile(cIniFileName)
else
Memo1.Lines.Clear;
end;
TMyIniFile
can be an alias for TIniFile
or a descendant of TMemIniFile
with an destructor calling UpdateFile
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不认为有“自动方法让 Windows 或 VCL 做到这一点”。我相信原因是“API”不知道您不打算再使用该部分(即,如果 API 在您删除最后一个键后删除该部分,那么有人会抱怨这效率低下)稍后向该部分添加密钥,因此必须重新创建该部分)。另外,有人可能会争辩说,空白部分的存在也携带了信息,因此,如果自动删除,该信息将会丢失。
I don't think there is "automatic way to make Windows or the VCL do that". I believe that the reason is that "API" doesn't know that you don't intend to use that section anymore (ie if the API would delete the section after you delete the last key someone would complain that this is inefficient as they intend to add an key to that section later and so the section must be recreated). Also, one could argue that the existence of the empty section carries information too and thus that information would be lost in case of automatic deletion.
我不相信
TIniFile
或TMemIniFile
会按照您想要的方式运行。因此,可能的解决方案是:DeleteKey
。UpdateFile
并删除此时的所有空部分。I don't believe that either
TIniFile
orTMemIniFile
will operate the way you want. Therefore the possible solutions are:DeleteKey
as you suggest.UpdateFile
and remove all empty sections at that point.