2013-07-01 49 views
1

我以前問過這個問題,但我試圖從註冊表中獲取一個Teamviewer ID,並在單擊按鈕時將其顯示在消息框中,但是當我單擊所述按鈕時,會彈出一個空白消息框並我想要解決這個問題的一些幫助。我如何在C#中獲得Teamviewer ID?

我的用於檢索Teamviewer ID的代碼如下所示;

public static string CollectTeamviewerId() 
     { 
      var versions = new[] { "4", "5", "5.1", "6", "7", "8" }.Reverse().ToList(); 

      foreach (var path in new[] { "SOFTWARE\\TeamViewer", "SOFTWARE\\Wow6432Node\\TeamViewer" }) 
      { 
       if (Registry.LocalMachine.OpenSubKey(path) != null) 
       { 
        foreach (var version in versions) 
        { 
         var subKey = string.Format("{0}\\Version{1}", path, version); 
         if (Registry.LocalMachine.OpenSubKey(subKey) != null) 
         { 
          var clientID = Registry.LocalMachine.OpenSubKey(subKey).GetValue("ClientID"); 
          if (clientID != null) 
          { 
           return clientID as string; 
          } 
         } 
        } 
       } 
      } 

併爲按鈕;

private void button4_Click(object sender, EventArgs e) 
     { 
      MessageBox.Show(LogDataFactory.CollectTeamviewerId()); 
     } 
+0

顯而易見的問題,而不是...如果你瀏覽到註冊表位置不關鍵存在,它包含的值? – Belogix

回答

1

代碼clientID as string更改爲clientID.ToString()在註冊表clientIDDWORD類型和clientID as string將永遠是null

if (clientID != null) 
{ 
     return clientID.ToString(); 
} 

編輯: 你可以檢查出as關鍵字here at MSDN

如果轉換是不可能的,因爲返回null拋出一個異常