Add a Network Printer

The .NET framework lacks the ability to add a Network Printer throughout it's various classes and functions. This snippet fills the gap as to how you can add a network printer programmatically. The snippet works by accessing the PrintUIEntry object in printui.dll via shell.

VB.NET

''' <summary>
''' Adds a Network Printer to the computer
''' </summary>
''' <param name="printerName">Name of the Printer you'd like to add</param>
''' <param name="networkPath">The path of the networked printer</param>
''' <param name="useExistingDriver">Don't overwrite existing driver</param>
''' <param name="setDefaultPrinter">Set as the default printer</param>
''' <remarks></remarks>

Public Shared Sub AddNetworkPrinter(ByRef printerName As String, ByRef networkPath As String, _
    Optional ByRef useExistingDriver As Boolean = 0, Optional ByRef setDefaultPrinter As Boolean = 0)

    Dim cmdToSend As String = "rundll32 printui.dll,PrintUIEntry /in /m " & Chr(34) & printerName & Chr(34) & " /f " & networkPath
    If useExistingDriver Then cmdToSend += " /u" '  /u = use the existing printer driver if it's already installed
    If setDefaultPrinter Then cmdToSend += " /y" '  /y = set printer as the default
    Shell(cmdToSend, AppWinStyle.Hide) ' execute the command

End Sub

' Example of how to use this subroutine:
AddNetworkPrinter("HP LaserJet 1320 PCL 6", "//myserver/Printer", True, True)

See Also

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License