[英]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语句中崩溃,则它看起来好像返回异常不是
ManagementException
或System.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.