I have been saving to the registry the infos that were typed into the TextBox on a form which was earlier created.After opening form, I want to appear all of the infos which was saved into the TextBox. When I attempt to run the following code,it's returning null value. What might be the problem?
Code:
SQLSERVER = textBox1.Text;
SQLDATABASE = textBox2.Text;
SQLUSER = textBox3.Text;
SQLPASS = textBox4.Text;
try
{
SqlConnection Baglanti = new SqlConnection("Data Source='" + SQLSERVER + "'; Initial Catalog='" + SQLDATABASE + "'; User id='" + SQLUSER + "'; Password='" + SQLPASS + "';");
Baglanti.Open();
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software", true);
if (key != null)
{
RegistryKey key2 = key.CreateSubKey("BilkerSoft");
key.SetValue("SQLSERVER", SQLSERVER, RegistryValueKind.String);
Registry.CurrentUser.CreateSubKey("BilkerSoft").SetValue("SQLSERVER", SQLSERVER);
Registry.CurrentUser.CreateSubKey("BilkerSoft").SetValue("DATABASE", SQLDATABASE);
Registry.CurrentUser.CreateSubKey("BilkerSoft").SetValue("USER", SQLUSER);
Registry.CurrentUser.CreateSubKey("BilkerSoft").SetValue("PASSWORD", SQLPASS);
}
}
catch (Exception ex)
{
MessageBox.Show("Hata oluştu:'" + ex.Message + "'");
}
RegistryKey key1 = Registry.CurrentUser.OpenSubKey("BilkerSoft",true);
try
{
if (key1 != null)
{
key1.SetValue("SQLSERVER", SQLSERVER, RegistryValueKind.String);
Registry.CurrentUser.OpenSubKey("BilkerSoft").GetValue("SQLSERVER", SQLSERVER);
Registry.CurrentUser.OpenSubKey("BilkerSoft").GetValue("DATABASE", SQLDATABASE);
Registry.CurrentUser.OpenSubKey("BilkerSoft").GetValue("USER", SQLUSER);
Registry.CurrentUser.OpenSubKey("BilkerSoft").GetValue("PASSWORD", SQLPASS);
}
Baglanti = new SqlConnection("Data Source='" + SQLSERVER + "';Initial Catalog='" + SQLDATABASE + "';User id='" + SQLUSER + "';Password='" + SQLPASS + "'");
Baglanti.Open();
Baglanti.Close();
MessageBox.Show("Kayıt Başarılı");
}
catch (Exceptio开发者_如何学Cn ex)
{
MessageBox.Show("Hata oluştu:'" + ex.Message + "'");
}
}
private void Form1_Load(object sender, EventArgs e)
{
RegistryKey key2 = Registry.CurrentUser.OpenSubKey("BilkerSoft", true);
textBox1.Text = Registry.CurrentUser.OpenSubKey("BilkerSoft").GetValue("SQLSERVER", SQLSERVER).ToString();
textBox2.Text = Registry.CurrentUser.OpenSubKey("BilkerSoft").GetValue("DATABASE", SQLDATABASE).ToString();
textBox3.Text = Registry.CurrentUser.OpenSubKey("BilkerSoft").GetValue("USER", SQLUSER).ToString();
textBox4.Text = Registry.CurrentUser.OpenSubKey("BilkerSoft").GetValue("PASSWORD", SQLPASS).ToString();
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
So, when you make the call Registry.CurrentUser.OpenSubKey("BilkerSoft", true);
, what is that a SubKey of exactly? When you made the subkey, you were creating it off of the value of the subkey "Software" but then you're trying to find one that doesn't reference that Software subkey... so it doesn't see it, right?
So you've got:
->Software
->BilkerSoft
But what you're initially looking for via key2 is:
->BilkerSoft
(A subkey off the root, which does not exist)
I suspect that if you qualify where you're looking for the subkey, it'll find it just fine. "BilkerSoft" is under "Software", not at the same level as "Software".
Meaning, that if you did something like key2 = key.OpenSubKey("BilkerSoft", true);
it would find it (since key
is the "Software" registry key). Haven't test this - I'm on a Mac - but seems like what you want.
void ReadReg(string key, params Action<RegistryKey>[] results)
{
var k = Registry.CurrentUser.OpenSubKey(key);
if (k != null)
{
foreach (var item in results)
{
item(k);
}
k.Close();
}
}
void WriteReg(string key, params Action<RegistryKey>[] results)
{
var k = Registry.CurrentUser.OpenSubKey(key, true);
if (k != null) k = Registry.CurrentUser.CreateSubKey(key);
foreach (var item in results)
{
item(k);
}
k.Close();
}
Write ==>
WriteReg(@"Software\BilkerSoft", key =>
{
key.SetValue("SQLSERVER", textBox1.Text);
}, key =>
{
key.SetValue("DATABASE", textBox2.Text);
}, key =>
{
key.SetValue("USER", textBox3.Text);
}, key =>
{
key.SetValue("PASSWORD", textBox4.Text);
});
Read ==>
ReadReg(@"Software\BilkerSoft", key =>
{
textBox1.Text = key.GetValue("SQLSERVER").ToString();
},key =>
{
textBox2.Text = key.GetValue("DATABASE").ToString();
},key =>
{
textBox3.Text = key.GetValue("USER").ToString();
},key =>
{
textBox4.Text = key.GetValue("PASSWORD").ToString();
});
精彩评论