Note: Forums will be making significant UX changes to address key usability improvements surrounding search, discoverability and navigation. To learn more about these changes please visit the announcement which can be found HERE.
how to add regisstry entry programatically

Answered how to add regisstry entry programatically

  • Thursday, June 04, 2009 7:05 AM
     
     
    hi,

    i want add a registry entry programatically using my application...
    I mean if registry entry is not there when application starts running... then my application should add that registry...

    Then whenevr i want i should be able to get and set the value of that registry programatically in my application....

    How i can do it...?

    Thanks in advance...
    IamHuM

All Replies

  • Thursday, June 04, 2009 7:10 AM
     
     
    Look at the docs for the Registry class, there are examples there that will show you how it's done..
  • Thursday, June 04, 2009 7:26 AM
     
     Answered Has Code
    Below is a demo project.
    The value of Slider is stored in RegistryKey table and can be loaded.

    XAML
    <Window x:Class="_temple.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
        <Grid>
            <Slider Loaded="TaskBarPreviewSetting_Loaded" Name="TaskBarPreviewSetting"  Minimum="100" Maximum="400"  ValueChanged="Slider_ValueChanged" ></Slider>
            
        </Grid>
    </Window>
    

    C#
    using System;
    using System.Windows;
    using Microsoft.Win32;
    namespace _temple
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public bool BeLoaded = false;
            public Window1()
            {
                InitializeComponent();
            }
            private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
            {
                if (BeLoaded)
                {
                    string TaskBarPreviewValue = TaskBarPreviewSetting.Value.ToString("0");
                    RegistryKey regKeyAppRoot = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\RWD\\Settings");
                    regKeyAppRoot.SetValue("TaskbarPreviewSetting", TaskBarPreviewValue, RegistryValueKind.String);
                }
            }
            private void TaskBarPreviewSetting_Loaded(object sender, RoutedEventArgs e)
            {
                BeLoaded = true;
                RegistryKey regKeyAppRoot = Registry.CurrentUser.CreateSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\RWD\\Settings");
                if (regKeyAppRoot != null)
                {
                    string data = regKeyAppRoot.GetValue("TaskbarPreviewSetting", 400).ToString();
                    double value = Convert.ToDouble(data);
                    TaskBarPreviewSetting.Value = value;
                }
            }
        }
    }
    

    • Proposed As Answer by Tao Liang Thursday, June 04, 2009 7:26 AM
    • Marked As Answer by Harry ZhuModerator Wednesday, June 10, 2009 7:06 AM
    •  
  • Thursday, June 04, 2009 7:45 AM
     
     Answered
    Hi,
      You can read the registry value like the following.
             Registry.GetValue(@"HKEY_CURRENT_USER\Software\YourApp\Settings", "yourVariable", "valueToGet");
      For setting the value use it like following.
             Registry.SetValue(@"HKEY_CURRENT_USER\Software\YourApp\Settings", "yourVariable", "valueToBeSaved");
       It will create the entry if the entry is not there.

    -- Thanks Ajith R
  • Friday, June 05, 2009 3:55 AM
     
     
    hi,

    Thanks for the replies...

    One thing i didnt understood is what is the difference between using 'regKeyAppRoot.SetValue' and 'Registry.SetValue'...?



    Regards,
    IamHuM
  • Friday, June 05, 2009 5:08 AM
     
     Answered
    The Registry class exposes RegistryKey objects. Registry.SetValue() is a static way of doing the same thing with an instance of RegistryKey --- regKeyAppRoot.SetValue(). You are simply accomplishing the same thing either way you look at it, in this case. If you needed to set a large number of values, the instance method might give better performance than the static method.

    -Scosby