首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C#检查和删除注册表项问题

C#检查和删除注册表项问题
EN

Stack Overflow用户
提问于 2017-05-10 17:55:54
回答 1查看 962关注 0票数 1

我正在检查(并删除)注册表项。代码:

代码语言:javascript
复制
 RegistryKey registryKey = Registry.CurrentUser.OpenSubKey
                    ("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);

                string codeBase = Assembly.GetExecutingAssembly().CodeBase;


            if (registryKey != null)           //   <- !!!!!!!!! problem is here, i think
                {
                    registryKey.SetValue("MyApp", codeBase);
                }

                else
                {
                    registryKey.DeleteValue("MyApp");
                }

在创造价值之后,应用程序不会看到新的价值,也不会删除它。这个密码怎么了?谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-10 18:08:01

添加和删除注册表的操作看起来不错,只是if-else条件有问题。如果您想检查然后删除注册表项,您应该这样做:

代码语言:javascript
复制
string KeyName = @"HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
string valueName = "MyApp";
string codeBase = Assembly.GetExecutingAssembly().CodeBase;

if (Registry.GetValue(KeyName, valueName, null) != null)
{
     RegistryKey registryKey = Registry.CurrentUser
         .OpenSubKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
     registryKey.DeleteValue(valueName);
}

Registry.GetValue(KeyName, valueName, null)是一种更好的方法来执行空检查,而不是执行registryKey != null,因为registryKey只获得..CurrentVersion\\Run子项。相反,你应该按下你的应用程序的实际键(例如。MyApp)。

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43899594

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档