C# 是什么导致 System.Drawing.Printing.PrinterSettings.InstalledPrinters 抛出 Win32Exception“RPC 服务器不可用”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/734628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 23:07:40  来源:igfitidea点击:

What causes System.Drawing.Printing.PrinterSettings.InstalledPrinters to throw Win32Exception "RPC server not available"?

c#.netprinting

提问by Richard R

I have an application that I'm working on and I allow the user to select a printer to use as their printer while printing forms from the application. I'm using .NET 2.0

我有一个正在处理的应用程序,我允许用户在从应用程序打印表单时选择一台打印机作为他们的打印机。我正在使用 .NET 2.0

In the settings screen, I call

在设置屏幕中,我调用

System.Drawing.Printing.PrinterSettings.InstalledPrinters 

to get the list of available printers.

获取可用打印机的列表。

On a client's machine, the property throws an exception:

在客户端的机器上,该属性会引发异常:

System.ComponentModel.Win32Exception: The RPC server is unavailable

The client is reporting that windows shows his printer as 'ready', and the client can print test pages from the printer. But, I have not been able to reproduce this issue locally and I'm running out of ideas. Does anyone have any ideas what could cause this issue? Any ideas or directions to look would be helpful. Thanks

客户报告 Windows 显示他的打印机为“就绪”,并且客户可以从打印机打印测试页。但是,我无法在本地重现这个问题,而且我的想法已经不多了。有没有人有任何想法可能导致这个问题?任何想法或方向都会有所帮助。谢谢

采纳答案by KristoferA

If you can print, most likely a security issue. Otherwise I would have voted "dead spooler service" but more likely your app doesn't have the rights to communicate with the spooler service...

如果可以打印,很可能是安全问题。否则我会投票给“dead spooler service”,但更有可能的是您的应用程序无权与 spooler 服务进行通信......

回答by Bertvan

Could this have anything to do with Network Printers? RPC is Remote Procedure Call, perhaps this is used for detecting installed network-printers?

这可能与网络打印机有关吗?RPC 是远程过程调用,也许这是用于检测已安装的网络打印机?

After some googling on the error:

在对错误进行一些谷歌搜索之后:

The InstalledPrinters uses WMI to look for printers. WMI on its turn uses RPC.

InstalledPrinters 使用 WMI 来查找打印机。WMI 反过来使用 RPC。

Having WMI return an error of your kind could mean the following:

让 WMI 返回类似的错误可能意味着以下情况:

  • MSDN Support
  • It could have something to do with accounts: check local/network/system account for your application and try running your application under a registered account (user+password)
  • Also check forum post, solving this for an ASP.NET application: link
  • MSDN 支持
  • 它可能与帐户有关:检查您的应用程序的本地/网络/系统帐户并尝试在注册帐户(用户+密码)下运行您的应用程序
  • 另请查看论坛帖子,为 ASP.NET 应用程序解决此问题:链接

And a useful WMI resourcefor your problem.

以及针对您的问题的有用WMI 资源

回答by Shea

Either the RPC service is off or Windows Firewall is blocking it.

RPC 服务已关闭或 Windows 防火墙正在阻止它。

回答by JCallico

I had a similar problem using PrinterSettings.InstalledPrinters and the cause was that the application was running with "Partial Trust" permissions.

我在使用 PrinterSettings.InstalledPrinters 时遇到了类似的问题,原因是该应用程序以“部分信任”权限运行。

In Visual Studio, went to Project Properties\Security and selected "This is a full trust application".

在 Visual Studio 中,转到 Project Properties\Security 并选择“这是一个完全信任的应用程序”。

I'm not sure if this is going to help you but did it for me.

我不确定这是否会帮助你,但它是为我做的。

回答by Paul Farry

Another possibility is also that the PrintSpooler service has been set to Disabled

另一种可能是 PrintSpooler 服务已设置为 Disabled

回答by Tim Santeford

This same thing is happening to me in Windows 7. Stopping and starting the print spooler in servicesfixes until it happens again.

我在 Windows 7 中也发生了同样的事情。在服务中停止和启动打印后台处理程序会修复,直到它再次发生。

The following commands make it quick to do this:

以下命令可以快速执行此操作:

net stop "print spooler"
net start "print spooler"

Note: Run the command prompt as Admin. I made a batch file to do this because it happens 4-5 times a day during development.

注意:以管理员身份运行命令提示符。我制作了一个批处理文件来执行此操作,因为它在开发过程中每天发生 4-5 次。