我如何在C#中获得Teamviewer ID?

时间:2013-07-01 15:42:14

标签: c# events registry

我之前已经问过这个问题,但是我试图从注册表中获取一个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());
        }

1 个答案:

答案 0 :(得分:1)

将代码clientID as string更改为clientID.ToString(),因为注册表中的clientID DWORD 类型,clientID as string将始终为null

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

修改 您可以查看as关键字here at MSDN

  

如果无法进行转换,则返回null而不是引发异常