windows 用于存储用户可配置应用程序设置的注册表与 INI 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Registry vs. INI file for storing user configurable application settings
提问by Kurt W. Leucht
I'm a new Windows programmer and I'm not sure where I should store user configurable application settings. I understand the need to provide a user friendly means for the user to change application settings, like an Edit | Settings form or similar. But where should I store the values after the user hits the Apply button on that form?
我是一个新的 Windows 程序员,我不确定应该在哪里存储用户可配置的应用程序设置。我了解需要为用户提供一种用户友好的方式来更改应用程序设置,例如 Edit | 设置表格或类似的。但是,在用户点击该表单上的“应用”按钮后,我应该将值存储在哪里?
What are the pros and cons of storing settings in the Windows registry vs. storing them in a local INI file or config file or similar?
将设置存储在 Windows 注册表中与将它们存储在本地 INI 文件或配置文件或类似文件中的优缺点是什么?
采纳答案by Kurt W. Leucht
Pros of config file:
配置文件的优点:
- Easy to do. Don't need to know any Windows API calls. You just need to know the file I/O interface of your programming language.
- Portable. If you port your application to another OS, you don't need to change your settings format.
- User-editable. The user can edit the config file outside of the program executing.
- 很容易做到。不需要知道任何 Windows API 调用。你只需要知道你的编程语言的文件 I/O 接口。
- 便携的。如果您将应用程序移植到另一个操作系统,则无需更改设置格式。
- 用户可编辑。用户可以在程序执行之外编辑配置文件。
Pros of registry:
注册的优点:
- Secure. The user can't accidentally delete the config file or corrupt the data unless he/she knows about regedit. And then the user is just asking for trouble.
- I'm no expert Windows programmer, but I'm sure that using the registry makes it easier to do other Windows-specific things (user-specific settings, network administration stuff like group policy, or whatever else).
- 安全的。除非用户知道 regedit,否则他/她不会意外删除配置文件或损坏数据。然后用户只是在自找麻烦。
- 我不是专业的 Windows 程序员,但我确信使用注册表可以更轻松地执行其他特定于 Windows 的事情(特定于用户的设置、网络管理内容,如组策略,或其他任何内容)。
If you just need a simple way to store config information, I would recommend a config file, using INI or XML as the format. I suggest using the registry only if there is something specific you want to get out of using the registry.
如果您只需要一种简单的方式来存储配置信息,我会推荐一个配置文件,使用 INI 或 XML 作为格式。我建议仅当您想从使用注册表中获得一些特定的东西时才使用注册表。
回答by Lipis
Jeff Atwood has a great articleabout Windows' registry and why is better to use .INI files instead.
Jeff Atwood 有一篇关于 Windows 注册表以及为什么使用 .INI 文件更好的文章。
My life would be a heck of a lot easier if per-application settings were stored in a place I could easily see them, manipulate them, and back them up. Like, say... in INI files.
- The registry is a single point of failure. That's why every single registry editing tip you'll ever find starts with a big fat screaming disclaimer about how you can break your computer with regedit.
- The registry is opaque and binary. As much as I dislike the angle bracket tax, at least XML config files are reasonably human-readable, and they allow as many comments as you see fit.
- The registry has to be in sync with the filesystem. Delete an application without "uninstalling" it and you're left with stale registry cruft. Or if an app has a poorly written uninstaller. The filesystem is no longer the statement of record-- it has to be kept in sync with the registry somehow. It's a total violation of the DRY principle.
- The registry is monolithic. Let's say you wanted to move an application to a different path on your machine, or even to a different machine altogether. Good luck extracting the relevant settings for that one particular application from the giant registry tarball. A given application typically has dozens of settings strewn all over the registry.
如果每个应用程序的设置都存储在一个我可以轻松查看、操作和备份的地方,我的生活会轻松很多。比如,在 INI 文件中。
- 注册表是单点故障。这就是为什么您会找到的每一个注册表编辑技巧都以一个巨大的尖叫免责声明开始,说明如何使用 regedit 破坏您的计算机。
- 注册表是不透明的和二进制的。尽管我不喜欢尖括号税,但至少 XML 配置文件是合理的人类可读的,并且它们允许尽可能多的注释。
- 注册表必须与文件系统同步。删除一个应用程序而不“卸载”它,你就会留下陈旧的注册表。或者,如果应用程序的卸载程序编写得不好。文件系统不再是记录声明——它必须以某种方式与注册表保持同步。这完全违反了 DRY 原则。
- 注册表是单一的。假设您想将应用程序移动到机器上的不同路径,甚至完全移动到不同的机器。祝您好运,从巨大的注册表 tarball 中提取该特定应用程序的相关设置。给定的应用程序通常在注册表中散布着数十种设置。
回答by Chris Jester-Young
According to the documentation for GetPrivateProfileString, you should use the registry for storing initialisation information.
根据GetPrivateProfileString的文档,您应该使用注册表来存储初始化信息。
However, in so saying, if you still want to use .ini files, and use the standard profile APIs (GetPrivateProfileString
, WritePrivateProfileString
, and the like) for accessing them, they provide built-in ways to automatically provide "virtual .ini files" backed by the registry. Win-win!
但是,也就是说,如果您仍然想使用 .ini 文件,并使用标准配置文件 API(GetPrivateProfileString
、WritePrivateProfileString
等)来访问它们,它们会提供内置方法来自动提供由 .ini 文件支持的“虚拟 .ini 文件”注册表。双赢!
回答by Greg
There's a similar question herethat covers some of the pros and cons.
有一个类似的问题在这里覆盖一些利弊。
I would suggest not using the registry unless your application absolutely needs it. From my understanding, Microsoft is trying to discourage the use of the registry due to the flexibility of settings files. Also, I wouldn't recommend using .ini files, but instead using some of the built-in functionalityto .Net for saving user/app settings.
我建议不要使用注册表,除非您的应用程序绝对需要它。据我了解,由于设置文件的灵活性,Microsoft 正试图阻止使用注册表。此外,我不建议使用 .ini 文件,而是使用.Net 的一些内置功能来保存用户/应用程序设置。
回答by EvilTeach
Use of an ini file, in the same directory as the application, makes it possible to back it up with the application. So after you reload your OS, you simply restore the application directory, and you have your configuration the way you want it.
使用与应用程序位于同一目录中的 ini 文件可以将其与应用程序一起备份。因此,在您重新加载操作系统后,您只需恢复应用程序目录,然后按照您想要的方式进行配置。
回答by deadtime
I agree with Daniel. If it's a large application I think I'd do things in the registry. If it's a small application and you want to have aspects of it user-configurable without making a configuration form, go for a quick INI file.
我同意丹尼尔。如果它是一个大型应用程序,我想我会在注册表中做一些事情。如果它是一个小型应用程序,并且您希望在不制作配置表单的情况下让用户对其进行配置,请使用快速 INI 文件。
I usually do the parsing like this (if the format in the .ini file is option = value, 1 per line, comments starting with #):
我通常是这样解析的(如果.ini文件中的格式是option = value,每行1个,注释以#开头):
static void Parse()
{
StreamReader tr = new StreamReader("config.ini");
string line;
Dictionary<string, string> config = new Dictionary<string, string>();
while ((line = tr.ReadLine()) != null)
{
// Allow for comments and empty lines.
if (line == "" || line.StartsWith("#"))
continue;
string[] kvPair = line.Split('=');
// Format must be option = value.
if (kvPair.Length != 2)
continue;
// If the option already exists, it's overwritten.
config[kvPair[0].Trim()] = kvPair[1].Trim();
}
}
Edit: Sorry, I thought you had specified the language. The implementation above is in C#.
编辑:对不起,我以为你已经指定了语言。上面的实现是在 C# 中。
回答by On Freund
There's one more advantage to using an INI file over the registry which I haven't seen mentioned: If the user is using some sort of volume/file based encryption, they can get the INI file to be encrypted pretty easily. With the registry it will probably be more problematic.
在注册表上使用 INI 文件还有一个我没有提到的优点:如果用户使用某种基于卷/文件的加密,他们可以很容易地加密 INI 文件。使用注册表可能会更成问题。
回答by On Freund
As Daniel indicated, storing configuration data in the registry gives you the option to use Admin Templates. That is, you can define an Admin Template, use it in a Group Policy and administer the configuration of your application network-wide. Depending on the nature of the application, this can be a big boon.
正如 Daniel 指出的,在注册表中存储配置数据为您提供了使用管理模板的选项。也就是说,您可以定义一个管理模板,在组策略中使用它并在整个网络范围内管理应用程序的配置。根据应用程序的性质,这可能是一个很大的好处。
回答by Class Skeleton
The existing answers cover a lot of ground but I thought I would mention one other point.
现有的答案涵盖了很多方面,但我想我会提到另一点。
I use the registry to store system-wide settings. That is, when 2 or more programs need the exact same setting. In other words, a setting shared by several programs.
我使用注册表来存储系统范围的设置。也就是说,当 2 个或更多程序需要完全相同的设置时。换句话说,由多个程序共享的设置。
In all other cases I use a local config file that sits either in the same path as the executable or one level down (in a Configuration directory). The reasons are already covered in other answers (portable, can be edited with a text editor etc).
在所有其他情况下,我使用一个本地配置文件,该文件位于与可执行文件相同的路径中或向下一级(在配置目录中)。其他答案中已经涵盖了原因(便携式,可以使用文本编辑器等进行编辑)。
Why put system-wide settings into the registry? Well, I found that if a setting is shared but you use local config files you end up duplicating settings. This may mean you end up needing to change a setting in multiple places.
为什么要将系统范围的设置放入注册表?好吧,我发现如果共享设置但您使用本地配置文件,则最终会复制设置。这可能意味着您最终需要在多个位置更改设置。
For example, say Program A and Program B both point to the same database. You can have a "system-wide" registry setting for the connection string. If you want to point to a different database, you can change the connection string in one place, and both programs will now run against the other database.
例如,假设程序 A 和程序 B 都指向同一个数据库。您可以为连接字符串设置“系统范围”的注册表设置。如果要指向不同的数据库,可以在一个位置更改连接字符串,两个程序现在将针对另一个数据库运行。
Note - there is no point in using the registry in this way if two or more programs don't need to use the same values. Such as, Program A and Program B both needing a database connection string that maybe the same, but not always. For example, I want Program B to now use a test database but Program A should carry on using a production database.
注意 - 如果两个或多个程序不需要使用相同的值,以这种方式使用注册表是没有意义的。例如,程序 A 和程序 B 都需要一个可能相同但并不总是相同的数据库连接字符串。例如,我希望程序 B 现在使用测试数据库,但程序 A 应该继续使用生产数据库。
With the above example, you could have some local configuration override system-wide settings but it may start getting overly complicated for simple tasks.
在上面的例子中,你可以有一些本地配置覆盖系统范围的设置,但对于简单的任务,它可能开始变得过于复杂。
回答by Mark Ransom
The registry is optimized for quick access and easy update, and it's the only way to do certain Windows-specific things like associating with an extension. And you can ignore the argument about deleting a single directory to uninstall your program - Windows Vista won't let you modify files in the Program Files directory, so your config will need to go in a different folder anyway.
注册表针对快速访问和轻松更新进行了优化,它是执行某些特定于 Windows 的事情(例如与扩展程序关联)的唯一方法。并且您可以忽略关于删除单个目录以卸载程序的论点 - Windows Vista 不允许您修改 Program Files 目录中的文件,因此您的配置无论如何都需要进入不同的文件夹。
There's a general guideline for Windows programming - do things the way Microsoft expects you to, and your life will be a lot easier.
Windows 编程有一个通用指南 - 按照 Microsoft 期望的方式做事,您的生活会轻松很多。
That said, I can see the appeal of the INI file, and I wouldn't blame anyone for considering it.
也就是说,我可以看到 INI 文件的吸引力,我不会责怪任何人考虑它。