【发布时间】:2021-04-05 15:53:25
【问题描述】:
这是一个奇怪的现象。我有一个 Delphi 10.3 应用程序,它已开始将重复条目写入 TIniFile。
代码如下:
with TIniFile.Create(UserDatFileName) do
try
WriteInteger(SFormName, 'Top', AForm.Top);
WriteInteger(SFormName, 'Left', AForm.Left);
WriteInteger(SFormName, 'Height', AForm.Height);
WriteInteger(SFormName, 'Width', AForm.Width);
WriteString(SFormName, 'WindowState', SWindowState);
finally
Free;
end;
多年来一直运行良好。现在,突然之间,我得到这样的输出:
[fMainForm]
Top=0
Left=0
Height=556
Width=671
WindowState=wsMaximized
pnlNavigation.Width=165
TreeListcxTreeListModule.Width=161
Top=0
Left=0
Height=556
Width=671
WindowState=wsMaximized
pnlNavigation.Width=165
TreeListcxTreeListModule.Width=161
Top=0
Left=0
Height=556
Width=671
WindowState=wsMaximized
pnlNavigation.Width=165
TreeListcxTreeListModule.Width=161
... etcetera etcetera
关于为什么会发生这种情况的任何建议?我认为TIniFile 值对在每个部分中应该是唯一的?
【问题讨论】:
-
你能提供一个minimal reproducible example吗?因为它可能是环保的。
-
我认为这在理论上可能会在多个应用程序同时写入时发生。我从未见过这种确切的行为,但我见过表明 TIniFile(使用
WritePrivateProfileStringAPI)的错误,它只锁定文件的一部分以进行写入。所以也许,如果一个值不存在,多个应用程序可以同时添加它。 -
顺便说一句,我现在会使用
TMemIniFile。这是任何非 Windows 平台上的默认设置,并且在 Windows 上也能正常工作。甚至快一点。您可以一次刷新所有更改,而不是在每次写入时调用这个相对较慢的 API。 -
我猜是文件格式问题。使用十六进制编辑器查看文件,其中可能存在破坏 PrivateProfileString 函数的字符。
-
您是否尝试删除该文件,然后查看问题是否仍然存在...