开发者

Getting a value (on the right pane) from the registry

开发者 https://www.devze.com 2023-02-23 01:52 出处:网络
I\'m trying to get a value from the registry in Windows, which sits under several branches: HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\...

I'm trying to get a value from the registry in Windows, which sits under several branches:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\... 

Given the following code:

RegistryKey openSubKey = settings.OpenSubKey开发者_Python百科(
    @"\\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers\DefaultSpoolDirectory");

openSubKey.SetValue("printername", "");

Where settings is set to LocalMachine in Microsoft.Win32.Registry.

However, I'd get null after the first line of code. What gives?


Two mistakes, \HKEY_LOCAL_MACHINE doesn't belong there, you should use Registry.LocalMachine instead. And DefaultSpoolDirectory doesn't belong there, it is a value, not a key.

The SetValue() arguments do not look happy either. If you want to add a printer (don't) then you'll need to add a key, not a value.


try removing "\hkey_local_machine"


Richard's answer seems correct, but just to inform you a little further, you can access the LocalMachine scope of the registry through the Registry.LocalMachine property - check this MSDN link for details. For an example, try this:

var openSubKey = Registry.LocalMachine.OpenSubKey(
    @"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Print\Printers");
0

精彩评论

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