我之前已经问过这个问题,但是我试图从注册表中获取一个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 :(得分:1)
将代码clientID as string
更改为clientID.ToString()
,因为注册表中的clientID
为 DWORD 类型,clientID as string
将始终为null
:
if (clientID != null)
{
return clientID.ToString();
}
修改强>
您可以查看as
关键字here at MSDN
如果无法进行转换,则返回null而不是引发异常