C# 如何使用 .NET 安装打印机?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/818583/
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
How can I install a printer using .NET?
提问by ichiban
I have an .INF for a virtual printer that I need to install from a .NET Application. I have done this before using batch scripts, but I am looking for a snippet of code to do this in the .NET Framework.
我有一个虚拟打印机的 .INF,我需要从 .NET 应用程序安装它。我在使用批处理脚本之前已经这样做了,但我正在寻找一段代码来在 .NET Framework 中做到这一点。
There's nothing particular about the printer .INF, so any code that installs a printer from an INF in C# or VB.NET will work.
打印机 .INF 没有什么特别之处,因此任何从 C# 或 VB.NET 中的 INF 安装打印机的代码都可以工作。
采纳答案by Kevin Pullin
I believe this is possible via interop to native win32 APIs, but I've found its much, much easier just to use a System.Diagnostics.Process() to call into printui.dll via:
我相信这可以通过与本机 win32 API 的互操作来实现,但我发现仅使用 System.Diagnostics.Process() 通过以下方式调用 printui.dll 就容易多了:
rundll32.exe printui.dll,PrintUIEntry /?
rundll32.exe printui.dll,PrintUIEntry /?
Perhaps you're already using that in the mentioned batch script, but if not the parameters are documented here: PrintUI.DLL User's Guide and Reference
也许您已经在提到的批处理脚本中使用了它,但如果没有,参数记录在此处:PrintUI.DLL User's Guide and Reference
Just be sure to test it against all operation systems you need to support. Some options either do not exist in all Windows releases or have been renamed (although I think they're the more esoteric options - installing an .INF will likely work across the board).
请务必针对您需要支持的所有操作系统对其进行测试。一些选项要么在所有 Windows 版本中都不存在,要么已被重命名(虽然我认为它们是更深奥的选项 - 安装 .INF 可能会全面工作)。
回答by bsneeze
This is notthe ideal solution, but if nobody else answers, you can create a temp batch file and invoke that through C#/VB.NET.
这不是理想的解决方案,但如果没有其他人回答,您可以创建一个临时批处理文件并通过 C#/VB.NET 调用它。
Someone else will probably know a more natural way to do this.
其他人可能会知道一种更自然的方法来做到这一点。
回答by Foredecker
You will need to wrap the setup APIs using PINVOKE or a native COM object.
您将需要使用 PINVOKE 或本机 COM 对象包装设置 API。
回答by crawdog
You are going to want to look at the WMI objects available. These give you a finer control of the local machine settings. Take a look at the WMI code creatorfrom Microsoft, I believe this will generate some example code you can leverage to solve your problem.
您将要查看可用的 WMI 对象。这些使您可以更好地控制本地机器设置。看看微软的WMI 代码创建器,我相信这会生成一些示例代码,您可以利用它来解决您的问题。
回答by Bishoy
Administering Printer Settings in C# for Flexible Printing
see this article, it uses an MS Platform SDK DLL called PRNADMIN to manage printers, printer-drivers, printer-ports, ...etc.
请参阅本文,它使用名为 PRNADMIN 的 MS 平台 SDK DLL 来管理打印机、打印机驱动程序、打印机端口等。
I personally use it in a commercial project to install a printer driver and change the printer port to local port to intercept the Postscript. and it works like a Charm.
我个人在一个商业项目中使用它来安装打印机驱动程序并将打印机端口更改为本地端口以拦截Postscript。它就像一个魅力。
Alternatively you can use some pre-installed vbscripts that come with windows in C:\Windows\system32\Printing_Admin_Scripts and here is a an articles for them:
或者,您可以在 C:\Windows\system32\Printing_Admin_Scripts 中使用 Windows 附带的一些预安装的 vbscript,这里有一篇关于它们的文章:
回答by Douglas Anderson
I always revert to the following links to install printers 'programatically'.
我总是恢复到以下链接以“以编程方式”安装打印机。
http://support.microsoft.com/kb/314486
http://support.microsoft.com/kb/314486