public void SetComboBox() { try { string[] directorieStrings = Directory.GetFiles(FileTools.logDirPath); int cnt = 0; foreach (string s in directorieStrings) { Log_File_ComboBox.Items.Add(s); ++cnt; } Log_File_ComboBox.SelectedIndex = cnt - 1; } catch (ArgumentNullException) { //FileTools.WriteLineFile(DateTime.Now.ToString() + FileTools.exceptionFilePath, " 路径为空!!"); FileTools.WriteLineFile(FileTools.exceptionFilePath, " 路径为空!!"); } catch (DirectoryNotFoundException) { FileTools.WriteLineFile(FileTools.exceptionFilePath, " 路径为空!!"); } }
//发送文件 public static bool SendFile(string printerName, string filePath) { try { if (printer != null && filePath != "") { PrintDocument pd = new PrintDocument(); pd.DefaultPageSettings.PrinterSettings.PrinterName = printerName; pd.DefaultPageSettings.Landscape = false; pd.PrintPage += (sender, args) => { //从指定文件里创建图片 System.Drawing.Image i = System.Drawing.Image.FromFile(filePath); args.Graphics.DrawImageUnscaled(i, 0, 0); }; pd.Print(); } } catch (InvalidPrinterException) { FileTools.WriteLineFile(FileTools.exceptionFilePath, printerName + "无效的打印机设置!"); return(false); } catch (System.ComponentModel.Win32Exception) { FileTools.WriteLineFile(FileTools.exceptionFilePath, printerName + "给打印机的数据过小(访问拒绝)!"); return(false); } catch (System.IO.FileNotFoundException) { FileTools.WriteLineFile(FileTools.exceptionFilePath, printerName + "没找到指定文件或目录!"); return(false); } return(true); }
//选择模板按钮 private void ChoiceFileButtonClick(object sender, RoutedEventArgs e) { System.Windows.Controls.Button button = sender as System.Windows.Controls.Button; Template_TextBox.Text = FileTools.GetFileShortName(FileTools.OpenFile("*.png|*.png|*.jpg|*.jpg", "*.png|*jpg")); }
//开始手动打印 private void PrintClick(object sender, RoutedEventArgs e) { if (txtAutoPrintNumber.Text == "") { State_TextBox.Text = "请输入能效编号"; return; } PNumber = txtAutoPrintNumber.Text; //输入编号,加载图片 string result = this.LoadPicture(); if (result != "") { if (result == "继续打印") { MessageBoxResult msgResult = MessageBox.Show("找不到模板,是否继续打印?", "", MessageBoxButton.YesNo); State_TextBox.Text = "找不到模板"; if (msgResult == MessageBoxResult.No) { return; } } else { State_TextBox.Text = result; return; } } //打印机操作 string[] data = new string[] { DateTime.Now.ToShortTimeString().ToString(), txtAutoPrintNumber.Text, Printer_List_ComboBox.Text }; foreach (string s in data) { if (s == "") { data = null; } } //发文件 uint num = uint.Parse(txtPrintCount.Text); while (num-- > 0) { bool isPrint; //记录是否打印,其实是记录是否异常,打印机不在线或者其他未发现的情况不视为异常,有待解决 if (_openImagePath == "") { isPrint = Printer.SendFile(Printer_List_ComboBox.Text, FileTools.FindFiles(FileTools.labelDirPath, EE_Number_TextBlock.Text, ".png")); } else { isPrint = Printer.SendFile(Printer_List_ComboBox.Text, _openImagePath); } //保存记录到日志文件 //时间戳,模板编码,手动输入的编码(亦是模板编码亦是能效编码,应该是能效编码,有待测试),打印机名称 if (isPrint) { if (textLogPage != null) { textLogPage.WriteDataToListView(data); } FileTools.WriteLineFile($"{FileTools.logDirPath}\\{DateTime.Now:yyyy-MM-dd}.txt", $"{data[0]};{PNumber};{data[1]};{data[2]};"); isWriteLog = true; } } }
//private ScanerHook listener = new ScanerHook(); public Window1() { InitializeComponent(); FileTools.Init(); textSearchPrints = new TextSearchPrints(this); }