.ini 文件未创建
我已经开始使用 .ini 文件,因为我意识到它与 .txt 文件相比有许多优点。无论如何,我在谷歌上搜索了“delphi inifiles”,现在正在遵循 Delphi About 的指南。然而,尽管所有语法都是正确的,我尝试的第一行却给我带来了麻烦。
Delphi About 的代码:
IniFile := TIniFile.Create('myapp.ini') ;
我的代码:
IniFile := TIniFile.Create('SWEDISH_HOUSE_MAFIA.ini');
唯一的区别是 .ini 文件本身的名称。另外,是的,我有:
- 在“uses”子句中声明“IniFiles”
- (本地)声明 IniFile
我已将该代码放置在 FormCreate 下,但问题是程序运行时不会创建此 .ini 文件。有人知道可能是什么问题吗?我问过朋友,他说是权限问题。
额外详细信息:
- 操作系统 - Windows 7 Ultimate 64 位(管理权限)
- 程序存储在 -
- 我使用的我的文档 - Delphi 2010
I have begun to start working with .ini files because I realise its many advantages compared to a .txt file. Any way, I googled "delphi inifiles" and I am now following a guide on Delphi About. However the very first line I tried gave me trouble even though all the syntax is right.
Delphi About's Code:
IniFile := TIniFile.Create('myapp.ini') ;
My code:
IniFile := TIniFile.Create('SWEDISH_HOUSE_MAFIA.ini');
The only difference is the name of the .ini file itself. Also, yes I have:
- Declared 'IniFiles' in the 'uses' clause
- (Locally) Declared IniFile
I have placed that code under FormCreate, but the problem is this .ini file is not created when the program is run. Anyone know what the problem could be? I've asked a friend about it and he said it was a permission issue.
Extra Details:
- OS - Windows 7 Ultimate 64 Bit (Administrative rights)
- Program is stored in - My Documents
- I use - Delphi 2010
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第一个问题是只有当您向 ini 文件写入内容时才会创建该文件。我怀疑目前您没有调用
WriteXXX
方法之一。另一个问题是,如果您没有限定路径,那么
TIniFile
将尝试在 Windows 目录中找到它,当然您无权在那里写入。TIniFile
所基于的底层 API 是私有配置文件 API,它已被长期弃用,性能非常糟糕,并且充满了奇怪的问题。 文档指出:显然,您应该完全限定您的路径。
不过,我强烈建议您考虑使用
TMemIniFile
而不是TIniFile
,因为TMemIniFile
避免了私有配置文件 API 的所有缺陷。如果您确实切换到
TMemIniFile
,请记住在销毁ini文件之前调用UpdateFile
,因为这会将设置保存到磁盘。否则,TMemIniFile
是TIniFile
的直接替代品。The first issue is that the file is only created when you write something to the ini file. I suspect that at present you aren't calling one of the
WriteXXX
methods.The other issue is that if you don't qualify your path then
TIniFile
will attempt to locate it in the Windows directory and of course you don't have rights to write there. The underlying API thatTIniFile
is based on is the private profile API which has been long deprecated, performs terribly, and is full of strange wrinkles. The documentation states:Clearly you should fully qualify your path.
However, I strongly recommend that you consider using
TMemIniFile
rather thanTIniFile
sinceTMemIniFile
avoids all the pitfalls of the private profile API.If you do switch to
TMemIniFile
then remember to callUpdateFile
before destroying the ini file since this is what will save the settings to the disk. OtherwiseTMemIniFile
is a drop-in replacement forTIniFile
.为了确保文件确实保存在磁盘上,
使用
To make sure the file is actually saved on disk,
use
%APPDATA%
路径。在 Windows 文件夹 (Win3..XP) 中创建不带路径的 INI。在 Vista/7 上,这些文件被重定向到\Users\[user]\some-magic-folder
(除非您的程序作为“安装程序”运行)%APPDATA%
path. INI without path is created in Windows folder (Win3..XP). On Vista/7 these files are redirected to\Users\[user]\some-magic-folder
(unless your program runs as "Installer")就像答案 3 所说,你只需要向 ini 写入一个值:) 这对我有用(win 7 Ultimate 64 位)。
Like answer 3 said, you just need to write a value to the ini :) This worked for me (win 7 ultimate 64 bit).