简体   繁体   English

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

[英]c# an unhandled exception of type 'System.Runtime.InteropServices.COMException'

I am running WMI queries but I need a way to test that the connection is working properly before running any queries. 我正在运行WMI查询,但是在运行任何查询之前,我需要一种方法来测试连接是否正常工作。

Below is the method that I created for to test the connection before running any query. 下面是我创建的用于在运行任何查询之前测试连接的方法。

Any ideas on how I can test the connection for failure? 关于如何测试连接失败的任何想法?

         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;
    }

If your program crashes inside the try catch statement, then it appears as if the return exception is not of type ManagementException or System.UnauthorizedAccessException 如果您的程序在try catch语句中崩溃,则它看起来好像返回异常不是ManagementExceptionSystem.UnauthorizedAccessException类型。

Try instead, simply using Catch(Exception ex) which should catch ALL exceptions, then trigger the error by mistyping information and see what error type it is, then handle that. 尝试改为使用Catch(Exception ex)来捕获所有异常,然后通过错误输入信息来触发错误并查看错误类型,然后进行处理。 Your error handling seems adequate with a message box and what I presume is kicking the routine back to the user to fix the input and resubmit, so I would mimic the same functionality with whatever error the system kicks out. 您可以使用一个消息框来进行错误处理,并且我认为是将例程踢回用户以修复输入并重新提交,因此无论系统出现什么错误,我都将模仿相同的功能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 c#Windbg中的System.Runtime.InteropServices.COMException错误 - c# System.Runtime.InteropServices.COMException error in windbg 'System.Runtime.InteropServices.COMException' - 'System.Runtime.InteropServices.COMException' System.Runtime.InteropServices.COMException - System.Runtime.InteropServices.COMException 未处理的异常:System.Runtime.InteropServices.COMException (0x800A03EC) - Unhandled Exception: System.Runtime.InteropServices.COMException (0x800A03EC) 发送邮件表单异常:System.Runtime.InteropServices.COMException:??? ?????? “SendUsing”???? ?÷? - Send mail form exception : System.Runtime.InteropServices.COMException: ??? ?????? “SendUsing” ???? ?÷? Puma .NET异常'System.Runtime.InteropServices.COMException' - Puma .NET exception 'System.Runtime.InteropServices.COMException' 尝试使用 C# 创建系统还原点时出现 System.Runtime.InteropServices.COMException - System.Runtime.InteropServices.COMException while trying to create a system restore point using C# Microsoft.Synchronization.dll中发生类型为“ System.Runtime.InteropServices.COMException”的第一次机会异常 - A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Synchronization.dll FAXCOMEX LIb的C#实现中的System.Runtime.InteropServices.COMException错误 - System.Runtime.InteropServices.COMException error in C# implementation of FAXCOMEX LIb vs.2010 C#中发生System.Runtime.InteropServices.COMException错误 - System.Runtime.InteropServices.COMException error occur in vs2010 c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM