Way to write on registry location

后端 未结 3 904
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-10 09:21

work on C# window application.I want to write on registry.i know how to write on registry.I use bellow syntax to write on registry.

        Microsoft.Win32.R         


        
相关标签:
3条回答
  • 2020-12-10 09:38
    RegistryKey reg = Registry.LocalMachine.
                      OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run", true);
    
    // set value of "abc" to "efd" 
    reg.SetValue("abc", "efd", RegistryValueKind.DWord);
    
    // get value of "abc"; return 0 if value not found 
    string value = (string)reg.GetValue("abc", "0");
    
    0 讨论(0)
  • 2020-12-10 09:39

    For HKCU:

    string keyName = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
    RegistryKey rk = Registry.CurrentUser.OpenSubKey(keyName, true);
    rk.SetValue("abc", "efd");
    rk.Close();
    

    For HKLM you need to do it with administrative privileges. That requires adding a manifest to your program to invoke the UAC prompt on Vista or Win7.

    0 讨论(0)
  • 2020-12-10 09:58

    Writing to HKEY_LOCAL_MACHINE requires administrative privileges. And if you're running on Windows Vista or 7, it also requires process elevation, lest you run afoul of UAC (User Account Control).

    The best thing is only to write to this registry key during installation (where you will have full administrative privileges). You should only read from it once your application is installed.

    Save all regular settings under HKEY_CURRENT_USER. Use the Registry.CurrentUser field to do that. Or, better yet, abandon the registry altogether and save your application's settings in a config file. Visual Studio has built-in support for this, it's very simple to do from C#. The registry is no longer the recommended way of saving application state.

    0 讨论(0)
提交回复
热议问题