How to check if a printer exists in C# and VB.NET

To check if a printer exists in C# and VB.NET you can use the following snippet.

Sample C#

public static bool PrinterExists(string printerName)
{
	if (String.IsNullOrEmpty(printerName)) {  throw new ArgumentNullException("printerName");}
	return PrinterSettings.InstalledPrinters.Cast<string>().Any(name => printerName.ToUpper().Trim() == name.ToUpper().Trim());
}

Sample VB.NET

Public Shared Function PrinterExists(printerName As String) As Boolean
	If String.IsNullOrEmpty(printerName) Then
		Throw New ArgumentNullException("printerName")
	End If
	Return PrinterSettings.InstalledPrinters.Cast(Of String)().Any(Function(name) printerName.ToUpper().Trim() = name.ToUpper().Trim())
End Function

How to list all printers of system in C# and VB.NET

To list all printers of system in C# and VB.NET you can use the following snippet.

Sample C#

for (var i = 0; i < System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count; i++)
{
	var printer = System.Drawing.Printing.PrinterSettings.InstalledPrinters[i];
	MessageBox.Show(printer);
}

Sample VB.NET

For i As var = 0 To System.Drawing.Printing.PrinterSettings.InstalledPrinters.Count - 1
	Dim printer = System.Drawing.Printing.PrinterSettings.InstalledPrinters(i)
	MessageBox.Show(printer)
Next

How to set the default printer in C# and VB.NET

To set the default printer in C# and VB.NET you can use the following snippet.

Sample C#

public static void SetDefaultPrinter(string printername)
{
	var type = Type.GetTypeFromProgID("WScript.Network");
	var instance = Activator.CreateInstance(type);
	type.InvokeMember("SetDefaultPrinter", System.Reflection.BindingFlags.InvokeMethod, null, instance, new object[] { printername });
}

Sample VB.NET

Public Shared Sub SetDefaultPrinter(ByVal printername As String)
        Dim type As var = Type.GetTypeFromProgID("WScript.Network")
        Dim instance As var = Activator.CreateInstance(type)
        type.InvokeMember("SetDefaultPrinter", System.Reflection.BindingFlags.InvokeMethod, Nothing, instance, New Object() {printername})
    End Sub