我一直在向注册表保存在早先创建的表单上键入TextBox的信息。打开表单后,我想显示保存到TextBox中的所有信息。当我尝试运行以下代码时,它返回null值。 可能是什么问题?
代码:
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 (Exception 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();
}
}
答案 0 :(得分:1)
那么,当你拨打电话Registry.CurrentUser.OpenSubKey("BilkerSoft", true);
时,究竟是什么SubKey?当你制作子项时,你是从子软件“Software”的值创建的,但是你试图找到一个不引用该子软件的子项...所以它没有看到了,对吧?
所以你有:
->Software
->BilkerSoft
但是你最初通过key2寻找的是:
->BilkerSoft
(根目录下的一个子键,不存在)
我怀疑如果你有资格在哪里寻找子项,它会发现它很好。 “BilkerSoft”位于“软件”下,与“软件”不在同一级别。
意思是,如果您执行类似key2 = key.OpenSubKey("BilkerSoft", true);
的操作,它会找到它(因为key
是“软件”注册表项)。没有测试过 - 我在Mac上 - 但看起来就像你想要的那样。
答案 1 :(得分:0)
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();
}
写==&gt;
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);
});
阅读==&gt;
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();
});