简体   繁体   English

无法在C#中设置注册表值

[英]Not able to set a registry value in C#

I have a windows form app called system_module . 我有一个名为system_module的Windows窗体应用程序。 And I want it to startup with windows. 我希望它使用Windows启动。 Here is my code for that. 这是我的代码。

private void Form1_Load(object sender, EventArgs e)
    {

        string keyName = @"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
        using (RegistryKey key = Registry.CurrentUser.OpenSubKey(keyName, true))
        {
            if (key == null)
            {
                // Key doesn't exist.
                key.SetValue("system_module", "Application Location");
            }
            else { }
        }
    }

But this is not creating a value in run/ I have searched the whole registry and found nothing referring to system_module except some irrelevant values. 但这不是在run中创建值/我已经搜索了整个注册表,除了一些不相关的值之外,没有找到任何引用system_module的信息。

PS I do not know much about registry things. PS我对注册表方面的知识不多。 Sorry if my terminology is wrong. 抱歉,如果我的术语错误。 Hope you understand what I am trying to to. 希望你理解我的意思。 I found the above code in some other question in this site. 我在此站点的其他问题中找到了上面的代码。 I don't know why this is not working. 我不知道为什么这不起作用。

You are not using the SetValue() - because the "Run"-Key exists 您没有使用SetValue()-因为存在“运行”键

use: 采用:

if (key != null)
        {
            // Key doesn't exist.
            key.SetValue("system_module", "Application Location");
        }

and handle the key == null too to add the Run key (by default this key exists) 并处理键== null来添加运行键(默认情况下,此键存在)

if you dont want to modify the key if your "system_module","Application Location" already exists you have to query the values beneath the "key" 如果您不希望在“系统模块”,“应用程序位置”已经存在的情况下修改密钥,则必须查询“密钥”下方的值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM