c#类型为'的未处理异常;System.Runtime.InteropServices.COMException';

c#类型为'的未处理异常;System.Runtime.InteropServices.COMException';,c#,error-handling,wmi,C#,Error Handling,Wmi,我正在运行WMI查询,但在运行任何查询之前,我需要一种方法来测试连接是否正常工作 下面是我为在运行任何查询之前测试连接而创建的方法 有没有关于如何测试连接失败的想法 private void btnQuery_Click(object sender, EventArgs e) { string strPC = txtPCName.Text.ToString(); string strDrive = txtDrive.Text.ToStri

我正在运行WMI查询,但在运行任何查询之前,我需要一种方法来测试连接是否正常工作

下面是我为在运行任何查询之前测试连接而创建的方法

有没有关于如何测试连接失败的想法

         private void btnQuery_Click(object sender, EventArgs e)
    {
        string strPC = txtPCName.Text.ToString();
        string strDrive = txtDrive.Text.ToString();

        if (strPC == "" || strDrive == "" || txtUser.Text.ToString() == "" || txtPW.Text.ToString() == "")
        {
            MessageBox.Show("The PC Name/ Drive Letter / Username / Or Password is blank. Please make sure all of these fields are filled out");
        }
        else
        {
            //Set up the connection first and test that it is working properly
            ConnectionOptions connection = new ConnectionOptions();
            connection.Username = txtUser.Text;
            connection.Password = txtPW.Text;
            connection.Authority = "ntlmdomain:expd";

            ManagementScope mgmtScope = new ManagementScope("\\\\" + strPC + "\\root\\cimv2", connection);

            if (pTestWMIConnection(mgmtScope) == true)
            {
                //Get PC Information
                double dblMem = pGetMemUsage("FreePhysicalMemory", mgmtScope);
                double dblTotDisk = pGetTotalDiskSpace(strDrive, mgmtScope);
                double dblFreeDisk = pGetFreeDiskSpace(strDrive, mgmtScope);

                txtMem.Text = Math.Round(dblMem, 2).ToString() + " MB";
                txtDriveTot.Text = Math.Round(dblTotDisk, 2).ToString() + " GB";
                txtFreeDisk.Text = Math.Round(dblFreeDisk, 2).ToString() + " GB";
                txtSN.Text = pGetWMIInfo("Win32_BIOS", "SerialNumber", mgmtScope);
                txtIP.Text = pGetIPAddress("Win32_NetworkAdapterConfiguration", "IPAddress", mgmtScope);
                txtModel.Text = pGetWMIInfo("Win32_ComputerSystem", "Model", mgmtScope);
                txtArch.Text = pGetWMIInfo("Win32_OperatingSystem", "OSArchitecture", mgmtScope);

                SetBalloonTip("Complete", "PC Query Completed");
            }
            else
            {
                MessageBox.Show("Connection to remote PC failed"); 
            }
        }           
    } 


 public static bool pTestWMIConnection(ManagementScope mgmtScope)
    {
        try
        {
            mgmtScope.Connect();

            if (mgmtScope.IsConnected == true)
            {
                return true;
            }
        }

        catch (ManagementException err)
        {
            MessageBox.Show("Unable to Return some or all of the information requested - " + err.Message);
            return false;
        }
        catch (System.UnauthorizedAccessException unauthorizedErr)
        {
            MessageBox.Show("Username or Password might be incorrect - " + unauthorizedErr.Message);
            return false;
        }
        return false;
    }

如果您的程序在try-catch语句中崩溃,则返回异常的类型似乎不是
ManagementException
System.UnauthorizedAccessException


相反,只需使用
Catch(Exception ex)
即可捕获所有异常,然后通过错误输入信息触发错误,查看错误类型,然后进行处理。对于消息框,您的错误处理似乎已经足够了,我认为是将例程返回给用户以修复输入并重新提交,因此无论系统出现什么错误,我都会模拟相同的功能。

我假设您的代码不工作?即使它返回true,在其他代码中仍然会出现异常?我们能看到其他代码吗?是的,正确。只有在正确输入PC名称的情况下,catch才起作用。假设用户在将其输入文本字段时出现键入错误,那么它将进入mgmtScope.Connect()并崩溃。是的,让我发布我的主要代码,谢谢你的帮助。我只需要添加异常错误处理程序