开发者

Is there anything like Environment.SpecialFolder but for registry paths

开发者 https://www.devze.com 2023-02-03 00:50 出处:网络
I want to store some app information in the registry and I\'m not sure where should I store it. I want the info to be for all users 开发者_运维技巧so I wouldn\'t use HKEY_CURRENT_USER. Maybe HKEY_LOCA

I want to store some app information in the registry and I'm not sure where should I store it. I want the info to be for all users 开发者_运维技巧so I wouldn't use HKEY_CURRENT_USER. Maybe HKEY_LOCAL_MACHINE but then I'm not sure where in there. I don't know what are the standards for this and something like Environment.SpecialFolder but for registry paths or folders would be a lot safer and more elegant.


Check the Microsoft.Win32.Registry object. There you can find some objects representing common registry paths, such as:

  • CurrentUser
  • LocalMachine
  • ClassesRoot
  • Users
  • PerformanceData
  • CurrentConfig
  • DynData

For example, if you want to access HKEY_CURRENT_USER:

Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Names");
key.SetValue("Name", "Isabella");
key.Close();


No -- unlike the filesystem, there's no need for such a method because the registry has somewhat standardized locations where things go.

You should store your data in a key following this format:

HKEY_HIVE\SOFTWARE\Publisher\Program

for example:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows

If you need data to be global to the entire machine, put your things inside HKEY_LOCAL_MACHINE, but note that writing to this location will require administrative rights unless you modify the DACLs on your key to behave differently (in some form of installer or something like that).

If you're storing per-user data (hint -- most data is per user), then you should put things in HKEY_CURRENT_USER, which has the advantage that no special rights are required for that user to access their hive (by default).

0

精彩评论

暂无评论...
验证码 换一张
取 消