Search code examples
c#buttonregistryregistrykey

C# - How can I get data from a registry key by a button.Name or button.Text?


I am creating an app in C# and want to load a registry key that the name is equal to the button.Name (or button.Text if not possible). Can someone illuminate me how to do this please?

public OptionsForm(Button btn)

{
// RegBtnName = Registry key
// RegBtnLink = Registry Key
// btnX = button
btnX = btn;
// AppName = Textbox
AppName.Text = Registry.GetValue(RegBtnName,Convert.ToString(btnX.Name),"Not Found");
// AppDir = TextBox
AppDir.Text = Registry.GetValue(RegBtnLink,Convert.ToString(btnX.Name),"Not Found");

InitializeComponent();

}

Solution

  • you can use -

     string keyName = btn.Name;
    
     var value = Registry.GetValue(keyName, 
            "NoSuchName",
            "Return this default if NoSuchName does not exist.");
    
     MessageBox.Show(value.ToString());
    

    You can find more info here