获取注册表
回答
使用Microsoft.Win32.Registry对象:
private Dictionary<string, object> GetRegistrySubKeys()
{
Dictionary<string, object> valuesBynames = new Dictionary<string, object>();
const string REGISTRY_ROOT = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\";
//Here I'm looking under LocalMachine. You can replace it with Registry.CurrentUser for current user...
using (RegistryKey rootKey = Registry.CurrentUser.OpenSubKey(REGISTRY_ROOT))
{
if (rootKey != null)
{
string[] valueNames = rootKey.GetValueNames();
foreach (string currSubKey in valueNames)
{
object value = rootKey.GetValue(currSubKey);
valuesBynames.Add(currSubKey, value);
}
}
rootKey.Close();
}
return valuesBynames;
}
确保添加相应的 “使用” 的声明:
using Microsoft.Win32;
随着词典<字符串,对象>我放什么?或者我如何使用你的代码? –
你只是'var registrykeys = GetRegistrySubKeys();'然后使用该变量你会[任何其他字典
谢谢,我的代码工作! –
如果键的值的字符串
string[] names = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\").GetSubKeyNames();
//dont call both at same time....
string[] values = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run\").GetValueNames();
foreach (string key in values)
{
//do your stuff...
}
需要导入命名空间
using Microsoft.Win32;
//also...
//retrieves the count of subkeys in the key.
int count = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion").SubKeyCount;
还要检查这篇文章,可以帮助你在你的任务 http://www.codeproject.com/Articles/3389/Read-write-and-delete-from-registry-with-C
编辑:
取自codeproject文章阅读的关键
public string Read(string KeyName)
{
// Opening the registry key
RegistryKey rk = baseRegistryKey ;
// Open a subKey as read-only
RegistryKey sk1 = rk.OpenSubKey(subKey);
// If the RegistrySubKey doesn't exist -> (null)
if (sk1 == null)
{
return null;
}
else
{
try
{
// If the RegistryKey exists I get its value
// or null is returned.
return (string)sk1.GetValue(KeyName.ToUpper());
}
catch (Exception e)
{
// AAAAAAAAAAARGH, an error!
ShowErrorMessage(e, "Reading registry " + KeyName.ToUpper());
return null;
}
}
}
调用'OpenSubKey'两次(或三次)是一个坏主意 - 如果注册表在两次调用之间发生变化(罕见但可能),您的代码将崩溃(或更糟!),因为两个数组大小不同,完全不同的数据。 – mcmonkey4eva
我已经将代码编辑为string [] names = Registry.CurrentUser.OpenSubKey(@“SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run”)。GetSubKeyNames(); string [] values = Registry.CurrentUser.OpenSubKey(@“SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run”)。GetValueNames(); foreach(字符串键值) MessageBox.Show(names +“,Value:”+ values);并且它的返回值是:System.String [],值:System.String [] –
... @DanielJones - 呃...你有任何C#经验吗?你似乎只是...做随机的东西......并不知道什么数组... ... – mcmonkey4eva
- 1. 获取注册表值C#
- 2. 获取注册表值
- 3. 使用C获取注册表值#
- 4. 获取远程注册表值
- 5. 使用Python获取注册表信息
- 6. 从注册表获取值问题
- 7. 骆驼 - 获取jndi注册表
- 8. 从注册表获取子目录
- 9. 从注册表C++崩溃获取值
- 10. 注册表项获取值返回NULL
- 11. 获取注册表项删除权限
- 12. 芹菜获取注册任务列表
- 13. 使用PHP注册表格获取
- 14. 获取注册表项的值
- 15. 从注册表中获取数据[C#]
- 16. 获取已注册的注释
- 17. 从注册表获得
- 18. 从注册表获得C#
- 19. VB.NET读取注册表
- 20. C++读取注册表
- 21. 注册表读取权限
- 22. 读取注册表项
- 23. 读取注册表项
- 24. 虽然注册获取注册日期时间和散列它
- 25. 取消注册DelegateCommand /获取使用的委托命令列表
- 26. 获取Windows服务来读取注册表HKLM
- 27. 推送注册表取消注册连接
- 28. 注册并取消注册angular js手表
- 29. pushwoosh注册并取消注册
- 30. DLL注册和取消注册
避免调用都在同一时间丹尼尔,这仅仅是为了演示(我不确切知道你需要什么)。 – terrybozzio