.ini 文件未创建

发布于 2024-12-03 20:52:07 字数 757 浏览 3 评论 0原文

我已经开始使用 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

稳稳的幸福 2024-12-10 20:52:07

第一个问题是只有当您向 ini 文件写入内容时才会创建该文件。我怀疑目前您没有调用 WriteXXX 方法之一。

另一个问题是,如果您没有限定路径,那么 TIniFile 将尝试在 Windows 目录中找到它,当然您无权在那里写入。 TIniFile 所基于的底层 API 是私有配置文件 API,它已被长期弃用,性能非常糟糕,并且充满了奇怪的问题。 文档指出:

如果 lpFileName 参数不包含文件的完整路径和文件名,WritePrivateProfileString 将在 Windows 目录中搜索该文件。如果该文件不存在,该函数将在 Windows 目录中创建该文件。

显然,您应该完全限定您的路径。

不过,我强烈建议您考虑使用 TMemIniFile 而不是 TIniFile,因为 TMemIniFile 避免了私有配置文件 API 的所有缺陷。

如果您确实切换到TMemIniFile,请记住在销毁ini文件之前调用UpdateFile,因为这会将设置保存到磁盘。否则,TMemIniFileTIniFile 的直接替代品。

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 that TIniFile is based on is the private profile API which has been long deprecated, performs terribly, and is full of strange wrinkles. The documentation states:

If the lpFileName parameter does not contain a full path and file name for the file, WritePrivateProfileString searches the Windows directory for the file. If the file does not exist, this function creates the file in the Windows directory.

Clearly you should fully qualify your path.

However, I strongly recommend that you consider using TMemIniFile rather than TIniFile since TMemIniFile avoids all the pitfalls of the private profile API.

If you do switch to TMemIniFile then remember to call UpdateFile before destroying the ini file since this is what will save the settings to the disk. Otherwise TMemIniFile is a drop-in replacement for TIniFile.

乖乖兔^ω^ 2024-12-10 20:52:07

为了确保文件确实保存在磁盘上,
使用

IniFile.UpdateFile

To make sure the file is actually saved on disk,
use

IniFile.UpdateFile
止于盛夏 2024-12-10 20:52:07
  1. 不要将 ini 保存到应用程序路径。使用 %APPDATA% 路径。在 Windows 文件夹 (Win3..XP) 中创建不带路径的 INI。在 Vista/7 上,这些文件被重定向到 \Users\[user]\some-magic-folder (除非您的程序作为“安装程序”运行)
  2. 使用 TMemFile 将大大加快操作速度。你需要调用UpdateFile/Free来更新文件,但它要快得多
  1. Don't save ini to application path. Use %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")
  2. Using TMemFile will speedup operations a lot. You need call UpdateFile/Free to update file, but it is much faster
初熏 2024-12-10 20:52:07
TForm1.btnWriteClick(Sender: TObject); 

var

    myINI : TINIFile;

begin 

    myINI := TINIFile.Create('Tryfolder\myini.ini');

    myINI.WriteString('Settings', 'Text Box', 'Whatever text');

    myINI.Free;

end;

就像答案 3 所说,你只需要向 ini 写入一个值:) 这对我有用(win 7 Ultimate 64 位)。

TForm1.btnWriteClick(Sender: TObject); 

var

    myINI : TINIFile;

begin 

    myINI := TINIFile.Create('Tryfolder\myini.ini');

    myINI.WriteString('Settings', 'Text Box', 'Whatever text');

    myINI.Free;

end;

Like answer 3 said, you just need to write a value to the ini :) This worked for me (win 7 ultimate 64 bit).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文