我想调用特定的打印机在我的WPF应用程序中打印.我有三台打印机假设打印机1用于条形码打印打印机2用于厨房帐单打印打印机3用于宾客帐单打印打印机名称已保存在数据库中,而打印时我从DB获取打印机名称并希望从特定打印机打印,而不是默认打印机这是我的码
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);
}
Run Code Online (Sandbox Code Playgroud)
我从这段代码中得到一个例外
"创建PrintServer对象时发生异常.Win32错误:打印机名称无效."
PrintServer必须是使用UNC格式的计算机或打印机服务器设备(\\resource)而不是打印机名称的实例:
例如,如果您域中的计算机名称是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
Run Code Online (Sandbox Code Playgroud)