No fancy formatting of the code, either! Ok, I will make the code a non-black colour.
The XAML code for the WPF form (as if I even know what XAML stands for!):
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="440" Width="633">
<Grid>
<Button x:Name="PrintSimpleTextButton" Content="Print File" HorizontalAlignment="Left" Height="22" Margin="10,95,0,0" VerticalAlignment="Top" Width="70" RenderTransformOrigin="-4.529,-2.864"/>
<Button x:Name="btnEnumeratePrinters" Content="Enumerate Printers" HorizontalAlignment="Left" Height="22" Margin="10,10,0,0" VerticalAlignment="Top" Width="135"/>
<ComboBox x:Name="cboPrinters" HorizontalAlignment="Left" Height="22" Margin="10,37,0,0" VerticalAlignment="Top" Width="218"/>
<TextBox x:Name="txtFilePath" HorizontalAlignment="Left" Height="26" Margin="10,64,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="359"/>
</Grid>
</Window>
The code for the form
Imports System.Printing
Imports System.IO
Class MainWindow
Private Sub PrintSimpleTextButton_Click(sender As Object, e As RoutedEventArgs) Handles PrintSimpleTextButton.Click
If cboPrinters.HasItems Then
If cboPrinters.SelectedIndex > -1 Then
Dim strPrinter As String
strPrinter = cboPrinters.Items(cboPrinters.SelectedIndex).ToString
If strPrinter.Length > 0 Then
Dim nextFile As String = txtFilePath.Text.Trim
If File.Exists(nextFile) Then
Try
Dim printque As PrintQueue
Dim printServer As New LocalPrintServer()
printque = printServer.GetPrintQueue(strPrinter)
' Print the Xps file while providing XPS validation and progress notifications.
Dim xpsPrintJob As PrintSystemJobInfo = printque.AddJob(Path.GetFileName(nextFile), nextFile, False)
Catch f As PrintJobException
Console.WriteLine(vbLf & vbTab & "{0} could not be added to the print queue.", Path.GetFileName(nextFile))
If f.InnerException.Message = "File contains corrupted data." Then
Console.WriteLine(vbTab & "It is not a valid XPS file. Use the isXPS Conformance Tool to debug it.")
End If
Console.WriteLine(vbTab & "Continuing with next XPS file." & vbLf)
End Try
End If
End If
End If
End If
End Sub
Private Function fwEnumeratePrinters() As PrintQueueCollection
' Specify that the list will contain only the print queues that are installed as local and are shared
Dim enumerationFlags() As EnumeratedPrintQueueTypes = {EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections}
Dim printServer As New LocalPrintServer()
'Use the enumerationFlags to filter out unwanted print queues
Dim printQueuesOnLocalServer As PrintQueueCollection = printServer.GetPrintQueues(enumerationFlags)
Return printQueuesOnLocalServer
End Function ' end:GetPrintTicketFromPrinter()
Private Sub btnEnumeratePrinters_Click(sender As Object, e As RoutedEventArgs) Handles btnEnumeratePrinters.Click
Dim pqPrinters As PrintQueueCollection
cboPrinters.Items.Clear()
pqPrinters = fwEnumeratePrinters()
For Each printer As PrintQueue In pqPrinters
cboPrinters.Items.Add(printer.Name)
Next
End Sub
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
txtFilePath.Text = "C:\Users\LIZ\Desktop\First Second Third.xps"
End Sub
End Class