2013-07-01 21 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抛出一个异常