Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invoke a specific printer to print C# WPF

Tags:

c#

wpf

I want to call a specific printer to print in my WPF application. I have three printer Suppose Printer1 for Bar bill Print Printer2 for Kitchen bill Print Printer3 for Guest bill Print printers name already saved in database, while printing I get a printer name from DB and want to print from specific printer, not defaul printer Here is my code

var v = new PrinterDAL().GetPrinterSettings();
try
{
   System.Threading.Thread thread = new System.Threading.Thread(new 
      System.Threading.ThreadStart(
            delegate()
            {   
                gridPrint.Dispatcher.Invoke(DispatcherPriority.Normal,
                    new Action(
                    delegate()
                    {
                            PrintDialog printDialog = new PrintDialog();
                            printDialog.PrintQueue = new PrintQueue(
                                new PrintServer(@"\\" + v.BarPrinter), "");
                            printDialog.PrintVisual(gridPrint, "");
                            this.Close();
                    }
                ));
            }
            ));
            thread.Start();
}
catch (Exception ex)
{
     Xceed.Wpf.Toolkit.MessageBox.Show(ex.Message, "", MessageBoxButton.OK, 
                                       MessageBoxImage.Error);
}

I get an exception from this code

"An exception occurred while creating the PrintServer object. Win32 error: The printer name is invalid."

like image 456
Kashif Hanif Avatar asked Jan 15 '23 01:01

Kashif Hanif


1 Answers

PrintServer must be instance using a computer or printer server device in UNC format (\\resource) not a printer name:

For example, if the name of your computer, in your domain, is KashifPC and you have configured a printer, called "Printer1", you can use:

//example code. no error handling.
PrintServer localPS = New PrintServer(@"\\KashifPC")
PrinterQueue printer1 = localPS.GetPrintQueue("Printer1") //v.BarPrinter???
PrintDialog printDialog = new PrintDialog();
printDialog.PrintQueue = printer1
//rest of code
like image 179
jlvaquero Avatar answered Jan 25 '23 06:01

jlvaquero