Esempio n. 1
0
 public static List <City> SetPrinters(ItemsControl itemsControl)
 {
     try
     {
         int          cnt  = 0;
         List <City>  list = new List <City>();
         string       filePathExecutePrinter     = FileTools.executePrintersFilePath;
         FileStream   fileStreamExecutePrinter   = new FileStream(filePathExecutePrinter, FileMode.OpenOrCreate, FileAccess.Read);
         StreamReader streamReaderExecutePrinter = new StreamReader(fileStreamExecutePrinter);
         string       line;
         string[]     data;
         while ((line = streamReaderExecutePrinter.ReadLine()) != null)
         {
             data = line.Split(';');
             GetPrinterStatus(data[0]);
             list.Add(new City
             {
                 ID         = ++cnt,
                 Name       = data[0],
                 Port       = data[1],
                 NetStatus  = ret,
                 WareStatus = rets == 0 ? "" : rets.ToString()
             });
         }
         printerCount = list.Count;
         streamReaderExecutePrinter.Close();
         itemsControl.ItemsSource = list;
         return(list);
     }
     catch (Exception e)
     {
         FileTools.WriteLineFile(FileTools.exceptionFilePath, DateTime.Now.ToString() + " " + e.Message);
         return(null);
     }
 }
Esempio n. 2
0
        //链接打印机
        public static string LinkPrinter(string printerName, int port)
        {
            try
            {
                string ip = GetRegistryData(printerName + "\\DsSpooler", "portName");

                if (ip == "" || ip.Split(new char[] { '.' }).Length != 4)
                {
                    throw new ConnectionException("没找到IP");
                }
                foreach (string s in ip.Split(new char[] { '.' }))
                {
                    int.Parse(s);
                }


                Ping      pingSender = new Ping();
                PingReply reply      = pingSender.Send(ip, 1);;
                if (reply.Status != IPStatus.Success)
                {
                    throw new ConnectionException("链接失败!");
                }



                /*Ping pingSender = new Ping();
                 * Thread thread = new Thread(new ThreadStart()));
                 * thread.Start();
                 * PingReply reply = null;
                 * reply = pingSender.Send(ip, 1);//第一个参数为ip地址,第二个参数为ping的时间
                 *
                 * Func<string, int, PingReply> func = new Func<string, int, PingReply>(Printer.PingIP);
                 * reply = func(ip, 1);
                 *
                 * if (reply.Status != IPStatus.Success)
                 *      throw new ConnectionException("链接失败!");*/

                TcpConnection connection = new TcpConnection(ip, port);
                connection.Open();
                printerHTTP   = ZebraPrinterFactory.GetInstance(connection);
                linkOsPrinter = ZebraPrinterFactory.CreateLinkOsPrinter(printerHTTP);
                printerStatus = printerHTTP.GetCurrentStatus();
            }
            catch (ConnectionException e)
            {
                printerHTTP = null;
                FileTools.WriteLineFile(FileTools.exceptionFilePath, DateTime.Now.ToString() + " " + printerName + e.Message);
                return(printerName + e.Message);
            }
            catch (FormatException e)
            {
                printerHTTP = null;
                FileTools.WriteLineFile(FileTools.exceptionFilePath, DateTime.Now.ToString() + " " + printerName + "IP地址不正确!");
                return(printerName + "IP地址不正确!");
            }

            return("");
        }
Esempio n. 3
0
        //发送文件
        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);
        }