Read Registry key and write in textbox the value
I have found the following code which reads and writes from the registry.
// Write a value to the registry
Microsoft.Win32.RegistryKey key;
key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey("Names");
key.SetValue("Name","Isabella");
// Get value from registry
key.GetValue("Name");
textBox1.Text = key.GetValue("Name"); // error, how can I do?
key.Close()开发者_如何学编程;
How would I display the value in a TextBox?
Add a call ToString()
after ensuring the value isn't null.
object oVal = key.GetValue("Name");
if (null != oVal)
{
textBox1.Text = oVal.ToString();
}
try
textBox1.Text = key.GetValue("Name").ToString();
GetValue returns an object and textbox1.Text wants a string so you need to convert first.
精彩评论