juF*_*uFo 5 c# wmi printers network-printers
这是server2003和2008r2上最近开始发生的一个例外:
ManagementException: Generic failure
Stack: at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
Run Code Online (Sandbox Code Playgroud)
收到此错误后,我还看到发生了此异常(后台处理程序停止了吗?):
The RPC server is unavailable
Win32Exception: The RPC server is unavailable
Stack: at System.Drawing.Printing.PrinterSettings.get_InstalledPrinters()
Run Code Online (Sandbox Code Playgroud)
此处列出了生成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();
}
// ...
Run Code Online (Sandbox Code Playgroud)
搜索后,我发现这些链接为我提供了可能的提示:
即使当我在PowerShell中尝试对远程PC进行此查询时:
Get-WmiObject -Query "SELECT Attributes, Name FROM Win32_Printer" -ComputerName "remotepc"
Run Code Online (Sandbox Code Playgroud)
一段时间后,我收到此错误:
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
Run Code Online (Sandbox Code Playgroud)
是否有任何WMI专家可以分享对C#代码的改进,因为目前我不知道为什么它偶尔会发生一次。