调用特定打印机来打印 C# WPF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13357009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:10:21  来源:igfitidea点击:

Invoke a specific printer to print C# WPF

c#wpf

提问by Kashif R

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

我想调用特定的打印机在我的 WPF 应用程序中打印。我有三台打印机假设打印机1用于酒吧账单打印打印机2用于厨房账单打印打印机3用于客人账单打印打印机名称已保存在数据库中,打印时我从数据库获取打印机名称并想从特定打印机打印,而不是默认打印机这是我的代码

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."

“创建 PrintServer 对象时发生异常。Win32 错误:打印机名称无效。”

回答by jlvaquero

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

PrintServer 必须是使用 UNC 格式 ( \\resource) 而不是打印机名称的计算机或打印机服务器设备的实例:

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

例如,如果您的计算机在您的域中的名称是 KashifPC 并且您配置了一台名为“Printer1”的打印机,您可以使用:

//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