简体   繁体   中英

How can I install a printer using .NET?

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.

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.

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:

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

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).

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 creator from Microsoft, I believe this will generate some example code you can leverage to solve your problem.

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.

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.

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:

http://technet.microsoft.com/en-us/library/cc771846.aspx

This is not the ideal solution, but if nobody else answers, you can create a temp batch file and invoke that through C#/VB.NET.

Someone else will probably know a more natural way to do this.

您需要使用PINVOKE或本机COM对象包装设置API。

I always revert to the following links to install printers 'programatically'.

http://support.microsoft.com/kb/314486

http://www.robvanderwoude.com/2kprintcontrol.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM