ManagementException:访问Win32_Printer的常规失败(导致RPC服务器不可用)

时间:2013-08-28 08:52:30

标签: c# wmi printers network-printers

这是最近在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();
}

// ...

在搜索之后,我发现这些链接给了我一个可能的提示:

  1. https://stackoverflow.com/a/5247725/187650
  2. http://social.msdn.microsoft.com/Forums/vstudio/en-US/b20c35f7-375b-4b67-af2e-bd432b6915da/how-to-retrieve-all-printer-names-installed-in-a-pc
  3. http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.wmi/2005-12/msg00097.html
  4. http://www.vistax64.com/powershell/134250-get-wmiobject-generic-failure.html
  5. 即使我尝试在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#代码的改进,因为此时我不知道为什么它只会偶尔发生一次。

0 个答案:

没有答案