2011-09-24 135 views
-1

我一直保存到注册表中的信息,这些信息是在早期创建的表单上的文本框中键入的。打开表单后,我想显示所有保存到文本框。当我尝试运行下面的代码时,它返回空值。 可能是什么问题?从文本框(C#)获取价值

代码:

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

您meen textBox1.Text == NULL? –

+0

哪一个是空的?键? key1的?他们全部? – itsmatt

+0

@ itsmatt-key1为空。 – Selo

回答

1

所以,当你拨打电话Registry.CurrentUser.OpenSubKey("BilkerSoft", true);,那是什么的正是一个子项?当您将设置为子项时,您将它创建为子项“Software”的值,但是您试图找到一个不引用该软件子项的项目......所以它没有看到它,对?

所以,你有:

->Software 
    ->BilkerSoft 

但你要首先通过KEY2寻找的是:

->BilkerSoft 

(A子项关闭根,不存在)

我怀疑如果你有资格在你要找的子项上,它会发现它很好。 “BilkerSoft”在“软件”下,与“软件”不在同一级别。

意思是,如果你做了类似key2 = key.OpenSubKey("BilkerSoft", true);的东西,它会发现它(因为key是“软件”注册表项)。没有测试过 - 我在Mac上 - 但看起来像你想要的。

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(); 
    } 

写==>

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); 
      }); 

阅读==>

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(); 
     });