Windows 注册表最佳实践

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/691448/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 12:13:39  来源:igfitidea点击:

Windows Registry best practices

windowsregistry

提问by Sean

In what way is the Windows registry meant to be used? I know it's alright to store a small amount of user preferences, but is it considered bad practice to store all your users data there? I would think it would depend on the data set, so how about for small amounts of data, say, less than 2KB, in 100 or so different key/value pairs. Is this bad practice? Would a flat file or SQLite db be a better practice?

打算以何种方式使用 Windows 注册表?我知道存储少量用户首选项没问题,但是将所有用户数据存储在那里是否被认为是不好的做法?我认为这将取决于数据集,那么对于少量数据,比如小于 2KB,在 100 个左右不同的键/值对中如何。这是不好的做法吗?平面文件或 SQLite db 会是更好的做法吗?

采纳答案by Mitchel Sellers

For me, simple user configuration items and user data is better to be stored in either a simple XML configuration file, a SQLLite db, or a MS SQL Server Compact db. The exact storage medium depends on the specifics of the implementation.

对我来说,简单的用户配置项和用户数据最好存储在简单的 XML 配置文件、SQLLite 数据库或 MS SQL Server Compact 数据库中。确切的存储介质取决于实现的细节。

I only use the registry for things that I need to set infrequently and that users don't need to be able to change/see. For example, I have stored encrypted license information in the registry before to avoid accidental user removal of the data.

我只将注册表用于我需要不经常设置并且用户不需要能够更改/查看的内容。例如,我之前在注册表中存储了加密的许可证信息,以避免用户意外删除数据。

回答by ReinstateMonica Larry Osterman

I'm going to take a contrarian view.

我将采取相反的观点。

The registry is a fine place to put configuration data of all types. In general it is faster than most configuration files and more reliable (individual operations on the registry are transacted so if your app crashes during a write the registry isn't corrupted - in general that isn't the case with ini files).

注册表是放置所有类型配置数据的好地方。一般来说,它比大多数配置文件更快,更可靠(注册表上的单个操作是事务性的,因此如果您的应用程序在写入期间崩溃,则注册表不会损坏 - 通常情况下 ini 文件并非如此)。

Marcelo MD is totally right: Storing things like operation percentage complete in the registry (or any other non volitile storage) is a horrible idea. On the other hand storing data like the most recently used files is just fine - the registry was built for just that kind of problem.

Marcelo MD 是完全正确的:在注册表(或任何其他非易失性存储)中存储诸如操作完成百分比之类的东西是一个可怕的想法。另一方面,像最近使用的文件一样存储数据也很好——注册表就是为这种问题而构建的。

A number of the other commenters on this post talking about the MRU list have discussed the problem of what happens when the MRU list gets out of sync due to application crashes. I'm wondering why storing the MRU list in a flat file in per-user storage is any better?

这篇关于 MRU 列表的许多其他评论者已经讨论了当 MRU 列表由于应用程序崩溃而失去同步时会发生什么的问题。我想知道为什么将 MRU 列表存储在每个用户存储的平面文件中会更好?

I'm also not sure what the "security implications" of storing your data in the registry are. The registry is just as secure as the filesystem - the registry and the filesystem use the same ACL mechanism to protect their data.

我也不确定将数据存储在注册表中的“安全隐患”是什么。注册表与文件系统一样安全——注册表和文件系统使用相同的 ACL 机制来保护它们的数据。

If you ARE going to store your user data in a file, you should absolutely put your data in %APPDATA%\CompanyName\ApplicationName at least - that way if two different developers create an application with the same name (how many "Media Manager" applications are there out there?) you won't have collisions.

如果您打算将您的用户数据存储在一个文件中,您绝对应该至少将您的数据放在 %APPDATA%\CompanyName\ApplicationName 中 - 如果两个不同的开发人员创建一个具有相同名称的应用程序(有多少“媒体管理器”那里有应用程序吗?)你不会有冲突。

回答by jalf

Using the registry to store data has mainly one problem: It's not very user-friendly. Users have virtually no chance of backing up their settings, copying them to another computer, troubleshooting them (or resetting them) if they get corrupted, or generally just seewhat their software is doing.

使用注册表来存储数据主要有一个问题:它不是很用户友好。用户几乎没有机会备份他们的设置,将它们复制到另一台计算机,如果他们被损坏,或者通常只是看看他们的软件在做什么,就没有机会对它们进行故障排除(或重置)。

My rule of thumb is to use the registry only to communicate with the OS. Filetype associations, uninstaller entries, processes to run at startup, those things obviously have to be in the registry.

我的经验法则是仅使用注册表与操作系统进行通信。文件类型关联、卸载程序条目、启动时运行的进程,这些显然必须在注册表中。

But data that is for use in your application only belongs in a file in your App Data folder. (whiever one of the 3+ App Data folders Microsoft currently wants you to use, anyway)

但是在您的应用程序中使用的数据只属于您的 App Data 文件夹中的一个文件。(无论如何,Microsoft 当前希望您使用的 3 个以上的 App Data 文件夹之一)

回答by itsmatt

As each user has directory space in Windows already dedicated to storing application user data, I use it to store the user-level data (preferences, for instance) there.

由于每个用户在 Windows 中都有专门用于存储应用程序用户数据的目录空间,我用它来存储用户级数据(例如首选项)。

In C#, I would get it by doing something like this:

在 C# 中,我会通过做这样的事情来得到它:

Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData);

Typically, I'll store SQLite files there or whatever is appropriate for the application.

通常,我会在那里存储 SQLite 文件或任何适合应用程序的文件。

回答by Cory R. King

If your app is going to be deployed "in the enterprise", keep in mind that administrators can tweak the registry using group policy tools. For example, if firefox used the registry for things like the proxy server, it would make deployment a snap because an admin can use the standard tools in active directory to set it up. If you use anything else, I dont think such things can be done very easily.

如果您的应用程序要“在企业中”部署,请记住管理员可以使用组策略工具调整注册表。例如,如果 firefox 将注册表用于代理服务器之类的东西,它会使部署变得轻而易举,因为管理员可以使用活动目录中的标准工具来设置它。如果你使用其他任何东西,我认为这样的事情不是很容易做到的。

So don't dismiss the registry all together. If there is a chance an admin might want to standardize parts of your configuration across a network, put the setting in the registry.

因此,不要完全关闭注册表。如果管理员可能希望通过网络标准化您的部分配置,请将设置放入注册表中。

回答by Treb

I would differentiate:

我会区分:

On the one hand there is application specific configuration data that is needed for the app to run, e.g. IP addresses to connect to, which folders to use for what sort of files etc, and non trivial per user settings. Those I put in a config file, ini format for simple stuff, xml if it gets more complex.

一方面,有应用程序运行所需的特定于应用程序的配置数据,例如要连接的 IP 地址、用于何种文件的文件夹等,以及每个用户的重要设置。那些我放在一个配置文件中的,简单的东西的 ini 格式,如果它变得更复杂,则为 xml。

On the other hand there is trivial per user settings (best example: window positions and layout). To avoid cluttering the config files (which some users will want to edit themselves, so few and clearly arranged entries are a must), I like to put those in the registry (with conservative defaults being set in the app if no settings in the registry can be found).

另一方面,每个用户的设置都很简单(最好的例子:窗口位置和布局)。为了避免混乱的配置文件(一些用户会想要自己编辑,所以必须少且排列清晰的条目),我喜欢将它们放在注册表中(如果注册表中没有设置,则在应用程序中设置保守的默认值可以被找寻到)。

I mainly do it like istmattsais: I store config files inside the %APPDATA%folder. Usually in %APPDATA%\ApplicationName, I don't like the .NET default of APPDATA%\CompanyName\ApplicationName\Version, that level of detail and complexity is counterproductive for most small to medium sized applications.

我主要是像istmattsais那样:我将配置文件存储在%APPDATA%文件夹中。通常在%APPDATA%\ApplicationName.NET中,我不喜欢 .NET 的默认设置 APPDATA%\CompanyName\ApplicationName\Version,这种详细程度和复杂性对于大多数中小型应用程序来说是适得其反的。

I disagree with the example of Marcelo MDof not storing recently used files in the registry. IMO this is exactly the volatile sort of user specific information that can be stored there. (His example of what notto do is very good, though!)

我不同意Marcelo MD不将最近使用的文件存储在注册表中的例子。IMO 这正是可以存储在那里的易变的用户特定信息。(不过,他的该做什么的例子非常好!)

回答by DOK

I think Microsoft is encouraging use of isolated storage instead of the Windows registry.

我认为微软鼓励使用隔离存储而不是 Windows 注册表。

Here'san article that explains how to use it in .Net.

是一篇解释如何在 .Net 中使用它的文章。

You can find those files in Windows XP under Documents & Settings\\Local Settings\ App Data\Isolated Storage. The data is in .dat files

您可以在 Windows XP 的 Documents & Settings\\Local Settings\ App Data\Isolated Storage 下找到这些文件。数据在 .dat 文件中

回答by Marcelo MD

To me it seems easier to think of what you should NOT put there. e.g: dynamic data, such as an editor's "last file opened" and per project options. It is really annoying when your app loses sync with the registry (file deletion, system crash, etc) and retrieves information that is not valid anymore, possibly deadlocking the user.

对我来说,想想你不应该把什么放在那里似乎更容易。例如:动态数据,例如编辑器的“最后打开的文件”和每个项目的选项。当您的应用与注册表失去同步(文件删除、系统崩溃等)并检索到不再有效的信息时,这真的很烦人,可能会导致用户死锁。

At an earlier job I saw a guy that stored a data transfer completness percentage there, Writing the new values at every 10k or so and having the GUI retrieve this value every second so it could show on the titlebar.

在早期的工作中,我看到一个人在那里存储数据传输完成百分比,每 10k 左右写入一次新值,并让 GUI 每秒检索该值,以便它可以显示在标题栏上。