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

Please rate this snippet

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

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

Samples

Sample C#

Sample VB.NET

Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5not working .NET 4.6

If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

Please rate this snippet

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

Please rate this snippet