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

标签 c# error-handling wmi

我正在运行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语句中崩溃,则看起来好像返回异常不是ManagementExceptionSystem.UnauthorizedAccessException类型

相反,尝试使用Catch(Exception ex)捕获所有异常,然后通过错误输入信息来触发错误并查看错误类型,然后进行处理。您可以使用一个消息框来进行错误处理,并且我认为是将例程踢回用户以修复输入并重新提交,因此无论系统出现什么错误,我都将模仿相同的功能。

关于c# - C#类型为 'System.Runtime.InteropServices.COMException'的未处理异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35801595/

相关文章:

c# - 使用 Double 数据类型传输 Money 值

php - 如何在codeigniter中重置问题 “The page is not redirecting properly”?

powershell - Msvm_ResourceAllocationSettingData类属性连接中的Hyper-V 2012 R2 WMI错误

c# - 从 C# 启动远程进程的问题

c# - 如何定义泛型类型的泛型集合类型

c# - 将一封或多封邮件从 Outlook 拖放到 C# WPF 应用程序

c# - 如何让 ToString() 出现在 Debug 中

perl - 如何正确处理 File::Slurp read_file 上的错误?

python - 类型错误 : unsupported operand type(s) for/: 'str' and 'int' Python 2. 7

c# - 授予远程用户(非管理员)使用 WMI 和 C# 枚举命名空间 cimv2 中 Win32_Service 中的服务的能力