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程序员,我不确定应该在哪里存储用户可配置的应用程序设置。我理解需要为用户提供用户友好的方法来更改应用程序设置,例如编辑|设置表单或类似。但是,在用户点击该表单上的“应用”按钮后,我应该在哪里存储值?
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文件或配置文件或类似文件中的优缺点是什么?
12 个解决方案
#1
38
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作为格式的配置文件。我建议只有在你想要使用注册表的某些特定内容时才使用注册表。
#2
23
Jeff Atwood has a great article about 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.
如果将每个应用程序设置存储在一个我可以轻松看到它们,操纵它们并备份它们的地方,那么我的生活将变得更容易。比如,说...在INI文件中。
- 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.
注册表是不透明的二进制文件。尽管我不喜欢尖括号税,但至少XML配置文件是人类可读的,它们允许你认为合适的评论。
- 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.
注册表必须与文件系统同步。删除一个应用程序而不“卸载”它,你就会留下陈旧的注册表。或者,如果某个应用程序的卸载程序写得不好。文件系统不再是记录语句 - 它必须以某种方式与注册表保持同步。这完全违反了DRY原则。
- 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.
注册表是单片的。假设您希望将应用程序移动到计算机上的其他路径,甚至将其移动到另一台计算机上。祝您从巨大的注册表tarball中提取该特定应用程序的相关设置。给定的应用程序通常有遍布注册表的数十个设置。
#3
4
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文件”的内置方法。由注册管理机构支持。双赢!
#4
4
There's a similar question here that 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 functionality to .Net for saving user/app settings.
我建议不要使用注册表,除非你的应用程序绝对需要它。根据我的理解,由于设置文件的灵活性,Microsoft正试图阻止使用注册表。此外,我不建议使用.ini文件,而是使用.Net的一些内置功能来保存用户/应用程序设置。
#5
4
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文件,可以使用应用程序对其进行备份。因此,在重新加载操作系统后,只需还原应用程序目录,就可以按照自己的方式进行配置。
#6
3
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文件。使用注册表可能会更有问题。
#7
2
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指出的那样,在配置文件中存储配置数据可让您选择使用管理模板。也就是说,您可以定义管理模板,在组策略中使用它并在网络范围内管理应用程序的配置。根据应用程序的性质,这可能是一个很大的好处。
#8
2
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#中。
#9
1
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编程的一般准则 - 以微软期望的方式做事,你的生活将变得更加容易。
That said, I can see the appeal of the INI file, and I wouldn't blame anyone for considering it.
也就是说,我可以看到INI文件的吸引力,我不会责怪任何人考虑它。
#10
1
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).
在所有其他情况下,我使用本地配置文件,该文件位于与可执行文件相同的路径中,或者位于一个级别(在Configuration目录中)。原因已在其他答案中介绍(便携式,可以使用文本编辑器等进行编辑)。
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 may be 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.
通过上面的示例,您可以使用一些本地配置覆盖系统范围的设置,但对于简单的任务,它可能会变得过于复杂。
#11
-1
There is one drawback to ini or config files and that is locating them if the user has the option to select where the program is installed.
ini或配置文件有一个缺点,即如果用户可以选择安装程序的位置,则可以找到它们。
#12
-2
Is your Application one that is installed with a Setup Program, or is it just "Extract and Run"? In the first case, look at the pros and cons outlined here. But for Extract and run, the Registry is in my opinion a "no-go", as people expect to be able to simply delete the application folder to get rid of your program.
您的应用程序是安装有安装程序的应用程序,还是只是“提取并运行”?在第一种情况下,请查看此处概述的优缺点。但对于Extract和run,在我看来,注册表是一个“禁止”,因为人们希望能够简单地删除应用程序文件夹以摆脱你的程序。
#1
38
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作为格式的配置文件。我建议只有在你想要使用注册表的某些特定内容时才使用注册表。
#2
23
Jeff Atwood has a great article about 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.
如果将每个应用程序设置存储在一个我可以轻松看到它们,操纵它们并备份它们的地方,那么我的生活将变得更容易。比如,说...在INI文件中。
- 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.
注册表是不透明的二进制文件。尽管我不喜欢尖括号税,但至少XML配置文件是人类可读的,它们允许你认为合适的评论。
- 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.
注册表必须与文件系统同步。删除一个应用程序而不“卸载”它,你就会留下陈旧的注册表。或者,如果某个应用程序的卸载程序写得不好。文件系统不再是记录语句 - 它必须以某种方式与注册表保持同步。这完全违反了DRY原则。
- 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.
注册表是单片的。假设您希望将应用程序移动到计算机上的其他路径,甚至将其移动到另一台计算机上。祝您从巨大的注册表tarball中提取该特定应用程序的相关设置。给定的应用程序通常有遍布注册表的数十个设置。
#3
4
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文件”的内置方法。由注册管理机构支持。双赢!
#4
4
There's a similar question here that 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 functionality to .Net for saving user/app settings.
我建议不要使用注册表,除非你的应用程序绝对需要它。根据我的理解,由于设置文件的灵活性,Microsoft正试图阻止使用注册表。此外,我不建议使用.ini文件,而是使用.Net的一些内置功能来保存用户/应用程序设置。
#5
4
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文件,可以使用应用程序对其进行备份。因此,在重新加载操作系统后,只需还原应用程序目录,就可以按照自己的方式进行配置。
#6
3
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文件。使用注册表可能会更有问题。
#7
2
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指出的那样,在配置文件中存储配置数据可让您选择使用管理模板。也就是说,您可以定义管理模板,在组策略中使用它并在网络范围内管理应用程序的配置。根据应用程序的性质,这可能是一个很大的好处。
#8
2
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#中。
#9
1
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编程的一般准则 - 以微软期望的方式做事,你的生活将变得更加容易。
That said, I can see the appeal of the INI file, and I wouldn't blame anyone for considering it.
也就是说,我可以看到INI文件的吸引力,我不会责怪任何人考虑它。
#10
1
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).
在所有其他情况下,我使用本地配置文件,该文件位于与可执行文件相同的路径中,或者位于一个级别(在Configuration目录中)。原因已在其他答案中介绍(便携式,可以使用文本编辑器等进行编辑)。
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 may be 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.
通过上面的示例,您可以使用一些本地配置覆盖系统范围的设置,但对于简单的任务,它可能会变得过于复杂。
#11
-1
There is one drawback to ini or config files and that is locating them if the user has the option to select where the program is installed.
ini或配置文件有一个缺点,即如果用户可以选择安装程序的位置,则可以找到它们。
#12
-2
Is your Application one that is installed with a Setup Program, or is it just "Extract and Run"? In the first case, look at the pros and cons outlined here. But for Extract and run, the Registry is in my opinion a "no-go", as people expect to be able to simply delete the application folder to get rid of your program.
您的应用程序是安装有安装程序的应用程序,还是只是“提取并运行”?在第一种情况下,请查看此处概述的优缺点。但对于Extract和run,在我看来,注册表是一个“禁止”,因为人们希望能够简单地删除应用程序文件夹以摆脱你的程序。