简体   繁体   English

在注册表中创建密钥

[英]Create key in registry

I am developing a WPF application.我正在开发一个 WPF 应用程序。 I want to create a key in the registry in the below path with key, value ( myapp.exe , 2710 ).我想在以下路径中的注册表中创建一个键,值( myapp.exe2710 )。

HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Internet Explorer\\MAIN\\FeatureControl\\FEATURE_BROWSER_EMULATION. HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Internet Explorer\\MAIN\\FeatureControl\\FEATURE_BROWSER_EMULATION。

Can any one suggest how to do this from the installer class, OnBeforeInstall method.任何人都可以从安装程序类OnBeforeInstall方法中建议如何执行此操作。

Regards问候

you can use below code for create a key and put value.您可以使用下面的代码来创建一个键和放置值。

 private string _subKey = "SOFTWARE\\Microsoft\\Internet Explorer\\MAIN\\FeatureControl\\FEATURE_BROWSER_EMULATION";
    private string SubKey
    {
        get { return _subKey; }
        set { _subKey = value; }
    }

    private RegistryKey _baseRegistryKey = Registry.LocalMachine;

    private RegistryKey BaseRegistryKey
    {
        get { return _baseRegistryKey; }
        set { _baseRegistryKey = value; }
    }

   private bool WriteDbToRegistry(string keyName, object value)
    {
        try
        {
            var rk = BaseRegistryKey;
            var sk1 = rk.CreateSubKey(SubKey);
            if(sk1 != null) sk1.SetValue(keyName.ToUpper(), value);
            return true;
        }
        catch(Exception e)
        {
            MessageBox.Show("Please run your App as Administrator.", "Administrator");
            return false;
        }
    }

 bool results = WriteDbToRegistry("myapp.exe", "2710");

hope this may be helpful.希望这可能会有所帮助。

Install the Microsoft.Win32.Registry NuGet package, and usse it like this:安装Microsoft.Win32.Registry NuGet 包,并像这样使用它:

    var keyName = @"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION";
    Registry.SetValue(keyName , "myapp.exe", 2710, RegistryValueKind.DWord);

Setting the value adds it if it's missing.如果缺少该值,则设置该值会添加它。

You can find more information in the documentation .您可以在文档中找到更多信息。

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

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