这是最近在server2003和2008r2上发生的例外:
ManagementException: Generic failure
Stack: at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
收到此错误后,我也看到此异常发生(假脱机程序已停止?):
The RPC server is unavailable
Win32Exception: The RPC server is unavailable
Stack: at System.Drawing.Printing.PrinterSettings.get_InstalledPrinters()
此处列出了生成WMI查询的代码:
// printername: \\server\printer or mylocalprinter
PrinterAttributes attribs = PrinterAttributes.Hidden;
ManagementObjectSearcher searchPrinter = null;
ManagementObjectCollection prntCol = null;
try
{
// "SELECT Attributes, Name FROM Win32_Printer"
string qry = string.Format("SELECT Attributes, Name FROM Win32_Printer WHERE Name = \'{0}\'", printerName);
searchPrinter = new ManagementObjectSearcher(qry);
prntCol = searchPrinter.Get();
foreach (ManagementObject prnt in prntCol) {
if (prnt["Name"].ToString() == printerName) { /* ... */ }
}
}
finally
{
if (prntCol != null) prntCol.Dispose();
if (searchPrinter != null) searchPrinter.Dispose();
}
// ...
在搜索之后,我发现这些链接给了我一个可能的提示:
即使我尝试在PowerShell中为远程PC执行此查询:
Get-WmiObject -Query "SELECT Attributes, Name FROM Win32_Printer" -ComputerName "remotepc"
我在一段时间后收到此错误:
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At line:1 char:1
+ Get-WmiObject -Query "SELECT Name FROM Win32_Printer" -ComputerName "remotepc"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
是否有任何WMI专家可以分享对C#代码的改进,因为此时我不知道为什么它只会偶尔发生一次。