.Net中获取打印机的相关信息
转载:http://www.cnblogs.com/JustYong/p/3861141.html
新项目中牵涉到对打印机的一些操作,最重要的莫过于获取打印机的状态,IP等信息,代码量不大,但是也是自己花了一点时间总结出来的,希望能帮助需要的朋友。
PrinterCommunicate用于连接打印机并发送指令
1 public class PrinterCommunicate
2 {
3
4 public bool CheckNetWorkConnection(string strPrinterIP, int intPrinterPort)
5 {
6 System.Net.Sockets.TcpClient Zebraclient = new TcpClient();
7 try
8 {
9 Zebraclient.Connect(strPrinterIP, intPrinterPort);
10 return Zebraclient.Connected;
11 }
12 catch
13 {
14 return false;
15 }
16 }
17
18 public bool SendZPL_ViaNetwork(string strPrinterIP, int intPrinterPort, string strPrinterCommand, out string strOutMsg)
19 {
20 strOutMsg = "";
21
22 System.Net.Sockets.TcpClient Zebraclient = new TcpClient();
23 try
24 {
25 Zebraclient.SendTimeout = 1500;
26 Zebraclient.ReceiveTimeout = 1500;
27 //defining ip address and port number
28 Zebraclient.Connect(strPrinterIP, intPrinterPort);
29
30 if (Zebraclient.Connected == true)
31 {
32 //send and receive illustrated below
33 NetworkStream mynetworkstream;
34 StreamReader mystreamreader;
35 StreamWriter mystreamwriter;
36 mynetworkstream = Zebraclient.GetStream();
37 mystreamreader = new StreamReader(mynetworkstream);
38 mystreamwriter = new StreamWriter(mynetworkstream);
39
40 mystreamwriter.WriteLine(strPrinterCommand);
41 mystreamwriter.Flush();
42 char[] mk = null;
43 mk = new char[256];
44 mystreamreader.Read(mk, 0, mk.Length);
45 string data1 = new string(mk);
46 strOutMsg = data1;
47 Zebraclient.Close();
48
49 return true;
50 }
51 else
52 {
53 strOutMsg = "Connection failed";
54 return false;
55 }
56 }
57 catch (Exception ex)
58 {
59 Log.WriteLogToFile("IPP_PCL", "PrinterCommunicate.cs -- SendZPL_ViaNetwork", "-99", ex.Message);
60 strOutMsg = "EXCEPTION_ERROR";
61 }
62
63 return false;
64 }
65
66 }
WindowsPrintQueue用于获取打印机的型号,以及得到打印机的WindowsPrintQueue
1 public class WindowsPrintQueue
2 {
3 /// <summary>
4 /// whether the two printer is same model.
5 /// </summary>
6 /// <param name="printerName1"></param>
7 /// <param name="printerName2"></param>
8 /// <returns></returns>
9 public bool IsSameModel(string printerName1, string printerName2)
10 {
11 return GetPrinterModel(printerName1).Equals(GetPrinterModel(printerName2));
12 }
13
14 /// <summary>
15 /// whether the printer is zebra model.
16 /// </summary>
17 /// <param name="printerName1"></param>
18 /// <param name="printerName2"></param>
19 /// <returns></returns>
20 public bool IsZebraPrinter(string printerName)
21 {
22 string zebraModel = "ZEBRA";
23 return GetPrinterModel(printerName).Contains(zebraModel);
24 }
25
26 /// <summary>
27 /// Return printer model
28 /// </summary>
29 /// <param name="printerName"></param>
30 /// <returns></returns>
31 public string GetPrinterModel(string printerName)
32 {
33 string model = string.Empty;
34 System.Printing.PrintQueue printQueue = GetPrintQueue(printerName);
35 if (printQueue != null)
36 {
37 //Get printer model
38 if (printQueue.Description.IndexOf(",") == printQueue.Description.LastIndexOf(","))
39 {
40 model = printQueue.Description.Substring(printQueue.Description.IndexOf(",") + 1, printQueue.Description.LastIndexOf(",") - printQueue.Description.IndexOf(",") - 1);
41 }
42 else
43 {
44 model = printQueue.Description.Substring(printQueue.Description.IndexOf(",") + 1);
45 }
46 }
47 return model;
48 }
49
50 /// <summary>
51 /// Get Windows Print Queue via printer name
52 /// </summary>
53 /// <param name="printerName"></param>
54 /// <returns></returns>
55 public System.Printing.PrintQueue GetPrintQueue(string printerName)
56 {
57 System.Printing.PrintQueue printQueue = null;
58 PrintServer server = new PrintServer(printerName);
59 foreach (System.Printing.PrintQueue pq in server.GetPrintQueues())
60 {
61 if (pq.FullName.Equals(printerName))
62 {
63 printQueue = pq;
64 }
65 }
66 return printQueue;
67 }
68
69 /// <summary>
70 /// Get Windows Print Queue via printer name
71 /// 如果两个printer指向的是同一个物理打印机,则如果copy1的printQueue已经打开, ///则发送到copy2的打印job也会添加到已经打开的copy1的printQueue中.
72 /// </summary>
73 /// <param name="printerName"></param>
74 /// <returns></returns>
75 public System.Printing.PrintQueue GetOpenedPrintQueueOfSameModel(string printerName)
76 {
77 System.Printing.PrintQueue doorOpenedprintQueue = null;
78 System.Printing.PrintQueue currentPrinterPrintQueue = null;
79 PrintServer server = new PrintServer(printerName);
80 foreach (System.Printing.PrintQueue pq in