1

以前にこの質問をしたことがありますが、レジストリから 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());
        }
4

1 に答える 1

1

レジストリはDWORD タイプであり、 常に次のようになるようにコードclientID as stringを変更します。clientID.ToString()clientIDclientID as stringnull

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

編集:ここの MSDN でasキーワード を確認できます

変換が不可能な場合、 as は例外を発生させる代わりに null を返します

于 2013-07-01T15:54:26.460 に答える