C# 如何决定在哪里存储每个用户的状态?注册表?应用程序数据?隔离存储?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/882490/
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
How to decide where to store per-user state? Registry? AppData? Isolated Storage?
提问by Cheeso
When should the Windows Registry be used for per-user state, and when should we use the filesystem, particularly the user's AppData folder? (eg, C:\Users\USERNAME\AppData). Where does Isolated Storage come in?
何时应将 Windows 注册表用于每个用户状态,何时应使用文件系统,尤其是用户的 AppData 文件夹?(例如,C:\Users\USERNAME\AppData)。隔离存储从何而来?
Is there a pretty firm rule, or is it just a fuzzy thing, like "use the registry until it becomes too much data to store in the registry". or "use whatever you feel like using."
是否有一个非常严格的规则,或者它只是一个模糊的东西,比如“使用注册表,直到它变得太多数据无法存储在注册表中”。或“使用任何你想使用的东西。”
Are there Windows logo requirements that affect the decision?
是否有影响决策的 Windows 徽标要求?
If I use the AppData directory, how do I choose between Local, Roaming and LocalLow ?
如果我使用 AppData 目录,如何在 Local、Roaming 和 LocalLow 之间进行选择?
edit: I just noticed these similar questions:
编辑:我刚刚注意到这些类似的问题:
- When and why should you store data in the Registry?
- Registry vs Ini file for storing user-configurable app settings.
I will summarize replies.
我将总结答复。
采纳答案by Nir
If you have a small number of key/value pairs and the values aren't big the registry is great - and you don't care about xcopy deployment - then use the registry (I know this isn't exact, but it's usually obvious when working with the registry becomes a pain).
如果您有少量的键/值对并且值不大,那么注册表很好 - 而且您不关心 xcopy 部署 - 然后使用注册表(我知道这不准确,但通常很明显当使用注册表时变得很痛苦)。
If you want xcopy deployment the data must be in the same folder as the program obviously - but the program can be somewhere under the AppData folder, it doesn't have to be under "program files".
如果您想要 xcopy 部署,数据显然必须与程序位于同一文件夹中 - 但程序可以位于 AppData 文件夹下的某个位置,它不必位于“程序文件”下。
Use isolated storage only when you need it or have to use it - for example ClickOnce.
仅在您需要或必须使用它时才使用隔离存储 - 例如 ClickOnce。
Otherwise use AppData\Roaming, use Local or LocalLow only if you have a good reason.
否则,请使用 AppData\Roaming,仅在您有充分理由时才使用 Local 或 LocalLow。
EDIT: Here is the difference between Roaming, Local and LocalLow:
编辑:这是漫游、本地和本地低之间的区别:
Windows has a little known feature called "roaming profiles", the general idea is that in a corporate environment with this feature enabled any user can use any computer.
Windows 有一个鲜为人知的功能,称为“漫游配置文件”,总体思路是在启用此功能的企业环境中,任何用户都可以使用任何计算机。
When a user logs in his private settings are downloaded from the server and when he logs out his settings are uploaded back to the server (the actual process is more complicated, obviously).
当用户登录时,他的私人设置从服务器下载,当他注销时,他的设置上传回服务器(显然,实际过程更复杂)。
Files in the User's "Roaming" folder in Vista or "Application Data" in XP move around with the user - so any settings and data should be stored there.
Vista 中用户“漫游”文件夹中的文件或 XP 中“应用程序数据”中的文件随用户移动 - 因此任何设置和数据都应存储在那里。
Files under "Local" and "LocalLow" in vista and "Local Settings" in XP do not, so it's a good place for temp files, things that are tied to the specific computer or data that can be recalculated.
vista 中“Local”和“LocalLow”下的文件以及 XP 中“Local Settings”下的文件没有,因此它是临时文件、与特定计算机相关的内容或可以重新计算的数据的好地方。
In Vista, as part of the new security features we all know and love, you can have programs running in "low integrity mode" (for example IE in protected mode), those programs are running with reduced privileges and can't access files in the user's profile - except for files under the "LocalLow" folder.
在 Vista 中,作为我们都知道和喜爱的新安全功能的一部分,您可以让程序以“低完整性模式”(例如处于保护模式的 IE)运行,这些程序以降低的权限运行并且无法访问其中的文件用户的个人资料 - “LocalLow”文件夹下的文件除外。
So, in conclusion, files stored in "LocalLow" are inherently insecure and files in "Local"/"Local Settings" are likely to be unavailable in some large companies - so unless you have good reason and know exactly what you are doing go with "Roaming"/"Application Data".
因此,总而言之,存储在“LocalLow”中的文件本质上是不安全的,“Local”/“Local Settings”中的文件在一些大公司中可能不可用 - 所以除非你有充分的理由并确切地知道你在做什么“漫游”/“应用程序数据”。
回答by BobbyShaftoe
You might want to consider Isolated Storage.
您可能需要考虑隔离存储。
回答by JMarsch
I don't know whether there is a firm rule, but but one thing to consider is that the registry is transacted -- it is safer for concurrent read/write operations. So, if your user data might be written by multiple threads at run time (or if you have multiple exe's in your product package), consider using the registry.
我不知道是否有严格的规则,但需要考虑的一件事是注册表是事务性的——对于并发读/写操作来说更安全。因此,如果您的用户数据可能在运行时由多个线程写入(或者如果您的产品包中有多个 exe),请考虑使用注册表。
History: One reason (as I heard it) that MS went from .ini files to the registry was precisely to try to handle the concurrent access problem.
历史:MS 从 .ini 文件转到注册表的一个原因(据我所知)正是为了尝试处理并发访问问题。
.Net (sort of) went back to .ini files in the form of xml .config files, however those config files are not supposed to be written to at runtime (or at least not if there is a chance of concurrent writers/readers).
.Net(有点)以 xml .config 文件的形式返回到 .ini 文件,但是这些配置文件不应该在运行时写入(或者至少如果有并发写入者/读取者的机会不会写入) .
More info: http://blogs.msdn.com/oldnewthing/archive/2007/11/26/6523907.aspx
更多信息:http: //blogs.msdn.com/oldnewthing/archive/2007/11/26/6523907.aspx
回答by Omar Kooheji
Don't clutter up my Registry thank you.
不要弄乱我的注册表谢谢。
Use isolated storage, thats what it's for.
使用隔离存储,这就是它的用途。
See Was The Windows Registry a Good Idea?On Jeffs Blog...
请参阅Windows 注册表是个好主意吗?在杰夫斯博客上...