C#如何检查注册表值是否存在? [英] C# How To Check If Registry Value Exist?

查看:1109
本文介绍了C#如何检查注册表值是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我想知道如何检查C#中是否存在Registry值。

i知道如何在VB中执行此操作:

Hello all.
I would like to know how to check if a Registry value exist in C#.
i know how to do this in VB:

If Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\Software\Number Averaging Program", "SpeechEnable", Nothing) Is Nothing Then
    MsgBox("There Are No Registry Settings To Delete!", Microsoft.VisualBasic.MsgBoxStyle.OkOnly, "Number Averaging Program")
Else
    Microsoft.Win32.Registry.CurrentUser.DeleteSubKey("Software\Number Averaging Program")
    MsgBox("Registry Settings Cleared Successfully!", Microsoft.VisualBasic.MsgBoxStyle.OkOnly, "Number Averaging Program")
End If





但是我怎么用C#做这个?

这是我到目前为止(它在C#中):



But how would i do this in C#?
Here is what i have so far(its in C#):

if (Microsoft.Win32.Registry.GetValue("HKEY_CURRENT_USER\\Software\\On Screen Keyboard", "SpeechSave", null) is Nullable)
{
    Registry.SetValue("HKEY_CURRENT_USER\\Software\\On Screen Keyboard\\Directories", "DictationEnabled", false);
}



感谢您的所有帮助。


Thanks for all of your help in advance.

推荐答案

你只需要添加使用到Microsoft.Win32命名空间(VB.NET项目由deafult),其余是相同的...



All you need is to add using to Microsoft.Win32 namespace (VB.NET project has it by deafult) and the rest is the same...

using Microsoft.Win32;

// ...

if (Registry.GetValue(@"HKEY_CURRENT_USER\Software\Key-Name", "Value-Name", null) == null)
{
  // ...
}


MasterCodeon365写道:
MasterCodeon365 wrote:



可以为Nullable


is Nullable



有你的问题 - 这不是你在C#中检查 null / Nothing 的方式。


There's your problem - that's not how you check for null / Nothing in C#.

if (Registry.GetValue("HKEY_CURRENT_USER\\Software\\Number Averaging Program", "SpeechEnable", null) == null)
{
    MessageBox.Show("There Are No Registry Settings To Delete!", "Number Averaging Program");
}
else
{
    Registry.CurrentUser.DeleteSubKey("Software\\Number Averaging Program");
    MessageBox.Show("Registry Settings Cleared Successfully!", "Number Averaging Program");
}


这篇关于C#如何检查注册表值是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆