原文:.Net中获取打印机的相关信息

新项目中牵涉到对打印机的一些操作,最重要的莫过于获取打印机的状态,IP等信息,代码量不大,但是也是自己花了一点时间总结出来的,希望能帮助需要的朋友。

PrinterCommunicate用于连接打印机并发送指令

 1     public class PrinterCommunicate
{ public bool CheckNetWorkConnection(string strPrinterIP, int intPrinterPort)
{
System.Net.Sockets.TcpClient Zebraclient = new TcpClient();
try
{
Zebraclient.Connect(strPrinterIP, intPrinterPort);
return Zebraclient.Connected;
}
catch
{
return false;
}
} public bool SendZPL_ViaNetwork(string strPrinterIP, int intPrinterPort, string strPrinterCommand, out string strOutMsg)
{
strOutMsg = ""; System.Net.Sockets.TcpClient Zebraclient = new TcpClient();
try
{
Zebraclient.SendTimeout = ;
Zebraclient.ReceiveTimeout = ;
//defining ip address and port number
Zebraclient.Connect(strPrinterIP, intPrinterPort); if (Zebraclient.Connected == true)
{
//send and receive illustrated below
NetworkStream mynetworkstream;
StreamReader mystreamreader;
StreamWriter mystreamwriter;
mynetworkstream = Zebraclient.GetStream();
mystreamreader = new StreamReader(mynetworkstream);
mystreamwriter = new StreamWriter(mynetworkstream); mystreamwriter.WriteLine(strPrinterCommand);
mystreamwriter.Flush();
char[] mk = null;
mk = new char[];
mystreamreader.Read(mk, , mk.Length);
string data1 = new string(mk);
strOutMsg = data1;
Zebraclient.Close(); return true;
}
else
{
strOutMsg = "Connection failed";
return false;
}
}
catch (Exception ex)
{
Log.WriteLogToFile("IPP_PCL", "PrinterCommunicate.cs -- SendZPL_ViaNetwork", "-99", ex.Message);
strOutMsg = "EXCEPTION_ERROR";
} return false;
} }

WindowsPrintQueue用于获取打印机的型号,以及得到打印机的WindowsPrintQueue

     public class WindowsPrintQueue
{
/// <summary>
/// whether the two printer is same model.
/// </summary>
/// <param name="printerName1"></param>
/// <param name="printerName2"></param>
/// <returns></returns>
public bool IsSameModel(string printerName1, string printerName2)
{
return GetPrinterModel(printerName1).Equals(GetPrinterModel(printerName2));
} /// <summary>
/// whether the printer is zebra model.
/// </summary>
/// <param name="printerName1"></param>
/// <param name="printerName2"></param>
/// <returns></returns>
public bool IsZebraPrinter(string printerName)
{
string zebraModel = "ZEBRA";
return GetPrinterModel(printerName).Contains(zebraModel);
} /// <summary>
/// Return printer model
/// </summary>
/// <param name="printerName"></param>
/// <returns></returns>
public string GetPrinterModel(string printerName)
{
string model = string.Empty;
System.Printing.PrintQueue printQueue = GetPrintQueue(printerName);
if (printQueue != null)
{
//Get printer model
if (printQueue.Description.IndexOf(",") == printQueue.Description.LastIndexOf(","))
{
model = printQueue.Description.Substring(printQueue.Description.IndexOf(",") + ,
printQueue.Description.LastIndexOf(",") - printQueue.Description.IndexOf(",") - );
}
else
{
model = printQueue.Description.Substring(printQueue.Description.IndexOf(",") + );
}
}
return model;
} /// <summary>
/// Get Windows Print Queue via printer name
/// </summary>
/// <param name="printerName"></param>
/// <returns></returns>
public System.Printing.PrintQueue GetPrintQueue(string printerName)
{
System.Printing.PrintQueue printQueue = null;
PrintServer server = new PrintServer(printerName);
foreach (System.Printing.PrintQueue pq in server.GetPrintQueues())
{
if (pq.FullName.Equals(printerName))
{
printQueue = pq;
}
}
return printQueue;
} /// <summary>
/// Get Windows Print Queue via printer name
/// 如果两个printer指向的是同一个物理打印机,则如果copy1的printQueue已经打开,
///则发送到copy2的打印job也会添加到已经打开的copy1的printQueue中.
/// </summary>
/// <param name="printerName"></param>
/// <returns></returns>
public System.Printing.PrintQueue GetOpenedPrintQueueOfSameModel(string printerName)
{
System.Printing.PrintQueue doorOpenedprintQueue = null;
System.Printing.PrintQueue currentPrinterPrintQueue = null;
PrintServer server = new PrintServer(printerName);
foreach (System.Printing.PrintQueue pq in server.GetPrintQueues())
{
if (pq.FullName.Equals(printerName))
{
currentPrinterPrintQueue = pq;
}
else
{
if (IsSameModel(printerName, pq.FullName))
{
if (pq.IsDoorOpened)
{
doorOpenedprintQueue = pq;
break;
}
}
}
} if (doorOpenedprintQueue != null)
{
return doorOpenedprintQueue;
}
else
{
return currentPrinterPrintQueue;
}
}
}

PrinterPropertyManager用于管理打印机的状态以及查询修改打印机属性

     class PrinterPropertyManager
2 {
/// <summary>
/// 获取打印机的IP地址和端口号
/// </summary>
/// <param name="printerName">打印机名称</param>
public KeyValuePair<string, int> GetPrinterIPAndPort(string printerName)
{
string port = GetPrinterPropertyValue(printerName, "PortName");
//Query portName's property from regedit
string[] portQuerys = GetPortQuerys(port);
foreach (var portQuery in portQuerys)
{
RegistryKey portKey = Registry.LocalMachine.OpenSubKey(portQuery, RegistryKeyPermissionCheck.Default,
System.Security.AccessControl.RegistryRights.QueryValues);
if (portKey != null)
{
object IPValue = portKey.GetValue("IPAddress", String.Empty,
RegistryValueOptions.DoNotExpandEnvironmentNames);
object portValue = portKey.GetValue("PortNumber", String.Empty,
RegistryValueOptions.DoNotExpandEnvironmentNames);
if (IPValue != null && portValue != null)
{
return new KeyValuePair<string, int>(IPValue.ToString(), (Int32)portValue);
}
}
}
return new KeyValuePair<string, int>();
} /// <summary>
/// determine whether the printer is network printer.
/// </summary>
public bool IsNetWorkPrinter(string printer)
{
string port = GetPrinterPropertyValue(printer, "PortName");
//Query portName's property from regedit
string[] portQuerys = GetNetWorkPortQuerys(port);
foreach (var portQuery in portQuerys)
{
RegistryKey portKey = Registry.LocalMachine.OpenSubKey(portQuery, RegistryKeyPermissionCheck.Default,
System.Security.AccessControl.RegistryRights.QueryValues);
if (portKey != null)
{
return true;
}
}
return false;
} private string[] GetNetWorkPortQuerys(string portName)
{
return new string[]
{
@"System\CurrentControlSet\Control\Print\Monitors\Advanced Port Monitor\Ports\" + portName,
@"System\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\" + portName
};
} private string[] GetPortQuerys(string portName)
{
return new string[]
{
@"System\CurrentControlSet\Control\Print\Monitors\Advanced Port Monitor\Ports\" + portName,
@"System\CurrentControlSet\Control\Print\Monitors\Local Port\Ports\" + portName,
@"System\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports\" + portName,
@"System\CurrentControlSet\Control\Print\Monitors\USB Monitor\Ports\" + portName,
@"System\CurrentControlSet\Control\Print\Monitors\WSD Port\Ports\" + portName,
};
} /// <summary>
/// get printer property value
/// </summary>
public string GetPrinterPropertyValue(string printerName, string propertyName)
{ string propertyValue = string.Empty;
//Query printer's portName from WIN32_Printer
string query = string.Format("SELECT * from Win32_Printer WHERE Name = '{0}'", printerName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection managementObjects = searcher.Get();
foreach (ManagementObject managementObject in managementObjects)
{
PropertyData propertyData = managementObject.Properties[propertyName];
if (propertyData != null)
{
propertyValue = propertyData.Value.ToString();
}
}
return propertyValue;
} /// <summary>
/// change printer property value
/// </summary>
public void SetPrinterPropertyValue(string printerName, string propertyName, string propertyValue)
{ //Query printer's portName from WIN32_Printer
string query = string.Format("SELECT * from Win32_Printer WHERE Name = '{0}'", printerName);
ManagementObjectSearcher searcher = new ManagementObjectSearcher(query);
ManagementObjectCollection managementObjects = searcher.Get();
foreach (ManagementObject managementObject in managementObjects)
{
PropertyData propertyData = managementObject.Properties[propertyName];
if (propertyData != null)
{
propertyData.Value = propertyValue;
managementObject.Put();
}
}
}

 /// <summary>
        /// whether the port is existed
/// 检查某个打印端口是否存在
        /// </summary>
        /// <param name="printerName"></param>
        /// <param name="port"></param>
        /// <returns></returns>
        public bool IsPortExisted(string printerName,string port)
        {
            string propertyName = "PortName";
            string currentPort = null;
            try
            {
                currentPort = GetPrinterPropertyValue(printerName, propertyName);
                SetPrinterPropertyValue(printerName, propertyName, port);
                SetPrinterPropertyValue(printerName, propertyName, currentPort);
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        } /// <summary>
/// 获取打印机名字的列表
/// </summary>
public ArrayList GetPrinterNames()
{
ArrayList result = new ArrayList(); foreach (string ss in PrinterSettings.InstalledPrinters)
{
result.Add(ss);
}
return result;
} /// <summary>
/// 获取打印机状态
/// </summary>
/// <param name="printerName">打印机名称</param>
public PrinterStatus GetPrinterStatus(string printerName,out bool isError,out string errorDescription)
{
//init return variable
isError = false;
errorDescription = string.Empty;
PrinterStatus printerStatus = PrinterStatus.Idle;
if (IsNetWorkPrinter(printerName))
{
KeyValuePair<string, int> ipPortKeyValuePair = GetPrinterIPAndPort(printerName);
PrinterCommunicate printerCommunicate = new PrinterCommunicate();
if (printerCommunicate.CheckNetWorkConnection(ipPortKeyValuePair.Key, ipPortKeyValuePair.Value))
{
WindowsPrintQueue winowsPrintQueue = new WindowsPrintQueue();
if (winowsPrintQueue.IsZebraPrinter(printerName))
{
//get actual status of zebra printer via zebra command
if(IsPause(ipPortKeyValuePair.Key, ipPortKeyValuePair.Value))
{
printerStatus = PrinterStatus.Paused;
} string errorMsg = string.Empty;
if(IsError(ipPortKeyValuePair.Key, ipPortKeyValuePair.Value, out errorMsg))
{
isError = true;
errorDescription = GetZebraPrinterErrorStatusDescription(errorMsg);
}
}
}
else
{
//not connected
printerStatus = PrinterStatus.Offline;
}
}
return printerStatus;
} /// <summary>
/// determine whether the network printer is in pause.Only for zebra model printer
/// </summary>
private bool IsPause(string ip, int port)
{
string strOutMsg = null;
string zebraCommand = "^XA~HS^XZ";
PrinterCommunicate printerCommunicate = new PrinterCommunicate();
if (printerCommunicate.SendZPL_ViaNetwork(ip, port, zebraCommand, out strOutMsg))
{
//split retMsg with "\r\n"
string[] retMsgs = strOutMsg.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
if (retMsgs != null)
{
string retFirstMsgItem = retMsgs[];
string[] retFirstMsgItems = retFirstMsgItem.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
return "".Equals(retFirstMsgItems[]);
}
}
return false;
} /// <summary>
/// determine whether the network printer is in error.only for zebra model printer
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <param name="strOutMsg"></param>
/// <returns></returns>
private bool IsError(string ip, int port, out string strOutMsg)
{
strOutMsg = string.Empty;
string zebraCommand = "^XA~HQES^XZ";
PrinterCommunicate printerCommunicate = new PrinterCommunicate();
if (printerCommunicate.SendZPL_ViaNetwork(ip, port, zebraCommand, out strOutMsg))
{
//split retMsg with "\r\n"
string[] retMsgs = strOutMsg.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
if (retMsgs != null)
{
for (int i = ; i < retMsgs.Length; i++)
{
string retMsgItem = retMsgs[i];
if (string.IsNullOrEmpty(retMsgItem) || !retMsgItem.Contains(":")) { continue; } string[] retMsgItemSplited = retMsgItem.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries);
if (retMsgItemSplited == null || retMsgItemSplited.Length == ) { continue; } string errorMsg = retMsgItemSplited[].Trim();
if (!string.IsNullOrEmpty(errorMsg))
{
string errorFlag = errorMsg.Substring(, );
if ("".Equals(errorFlag))
{
strOutMsg = errorMsg;
return true;
}
}
}
}
}
return false;
} /// <summary>
/// get actual status of zebra printer via zebra command.
/// </summary>
/// <param name="ip"></param>
/// <param name="port"></param>
/// <returns></returns>
private string GetZebraPrinterErrorStatusDescription(string errorMsg)
{
StringBuilder status = new StringBuilder();
//error happend
string nibble1 = errorMsg.Substring(errorMsg.Length - , );
string nibble2 = errorMsg.Substring(errorMsg.Length - , );
string nibble3 = errorMsg.Substring(errorMsg.Length - , ); if (!"".Equals(nibble1))
{
Dictionary<int, string> nibble1ErrorDictionary = new Dictionary<int, string>();
nibble1ErrorDictionary.Add(, "Midea Out");
nibble1ErrorDictionary.Add(, "Ribbon Out");
nibble1ErrorDictionary.Add(, "Head Open");
nibble1ErrorDictionary.Add(, "Cutter Fault"); status.Append(GetErrorDescriptionFromNibble(nibble1, nibble1ErrorDictionary));
} if (!"".Equals(nibble2))
{
Dictionary<int, string> nibble2ErrorDictionary = new Dictionary<int, string>();
nibble2ErrorDictionary.Add(, "Printhead Over Temperature");
nibble2ErrorDictionary.Add(, "Motor Over Temperature");
nibble2ErrorDictionary.Add(, "Bad Printhead Element");
nibble2ErrorDictionary.Add(, "Printhead Detection Error"); status.Append(GetErrorDescriptionFromNibble(nibble1, nibble2ErrorDictionary));
} if (!"".Equals(nibble3))
{
Dictionary<int, string> nibble3ErrorDictionary = new Dictionary<int, string>();
nibble3ErrorDictionary.Add(, "Invalid Firmware Config");
nibble3ErrorDictionary.Add(, "Printhead Thermistor Open"); status.Append(GetErrorDescriptionFromNibble(nibble1, nibble3ErrorDictionary));
} string strStatus = status.ToString();
return strStatus.Substring(, strStatus.Length - );
} private StringBuilder GetErrorDescriptionFromNibble(string nibble, Dictionary<int, string> statusDictionary)
{
int intNibble = Convert.ToInt32(nibble);
StringBuilder status = new StringBuilder();
if (statusDictionary != null)
{
foreach (var statusKV in statusDictionary)
{
if ((intNibble & statusKV.Key) == statusKV.Key)
{
status.Append(statusKV.Value);
status.Append(",");
}
}
}
return status;
}
} public enum PrinterStatus
{
Other = ,
Unknown = ,
Idle = ,
Printing = ,
Warmup = ,
Paused = ,
Offline =
}

.Net中获取打印机的相关信息的更多相关文章

  1. Java Web应用中获取用户请求相关信息,如:IP地址、操作系统、浏览器等信息

    引入jar包 <dependency> <groupId>eu.bitwalker</groupId> <artifactId>UserAgentUti ...

  2. SAP中获取当前用户相关信息的两个函数

    函数名:TH_USER_LIST作用:可以得到SM04界面显示样式的表. 函数名:TH_USER_INFO作用:可以得到当前特定用户的机器名.当前活动窗口数.IP地址等信息

  3. $_SERVER['HTTP_USER_AGENT']:在PHP中HTTP_USER_AGENT是用来获取用户的相关信息的,包括用户使用的浏览器,操作系统等信息

    在PHP中HTTP_USER_AGENT是用来获取用户的相关信息的,包括用户使用的浏览器,操作系统等信息. 我机器:操作系统:WIN7旗舰版 64操作系统 以下为各个浏览器下$_SERVER['HTT ...

  4. Cordova各个插件使用介绍系列(六)—$cordovaDevice获取设备的相关信息

    详情请看:Cordova各个插件使用介绍系列(六)—$cordovaDevice获取设备的相关信息 在项目中需要获取到当前设备,例如手机的ID,联网状态,等,然后这个Cordova里有这个插件可以用, ...

  5. 获取Java系统相关信息

    package com.test; import java.util.Properties; import java.util.Map.Entry; import org.junit.Test; pu ...

  6. C#获取电脑的相关信息

    /* 创建者:菜刀居士的博客  * 创建日期: 2014年08月31号  * 功能:获取电脑的相关信息  *  */ namespace Net.String.ConsoleApplication { ...

  7. 通过request对象获取客户端的相关信息

    通过request对象获取客户端的相关信息 制作人:全心全意 通过request对象可以获取客户端的相关信息.例如HTTP报头信息.客户信息提交方式.客户端主机IP地址.端口号等等. request获 ...

  8. NX二次开发-UF_ASSEM_ask_component_data获取装配部件的相关信息

    NX9+VS2012 #include <uf.h> #include <uf_ui.h> #include <uf_assem.h> ], void* user_ ...

  9. MSSQL·查询数据库中所有索引的相关信息

    阅文时长 | 0.45分钟 字数统计 | 784字符 主要内容 | 1.引言&背景 2.声明与参考资料 『MSSQL·查询数据库中所有索引的相关信息』 编写人 | SCscHero 编写时间 ...

随机推荐

  1. JavaScript 中的事件类型5(读书笔记思维导图)

    Web 浏览器中可能发生的事件有很多类型.如前所述,不同的事件类型具有不同的信息,而“ DOM3级事件”规定了以下几类事件. UI(User Interface,用户界面)事件:当用户与页面上的元素交 ...

  2. 关于JAVA Project.waitfor()返回值是1

    Project.waitfor()返回值是1,找了很久从网上没有发现关于1的说明. 这时对源代码调试了一下,发现Project=null.而去根目录下点击被调用的bat文件发现也可以被正确执行. 这时 ...

  3. graphterm 0.40.1 : Python Package Index

    graphterm 0.40.1 : Python Package Index graphterm 0.40.1 Downloads ↓ A Graphical Terminal Interface ...

  4. DataTable数据转换为实体

    我们在用三层架构编写软件时,常常会遇到例如以下问题,就是三层之间的參数传递问题:假设我们在D层查询出数据是DataTable类型的,那么我们在B层甚至U层使用这条数据时,就要用DataTable类型来 ...

  5. DOM中的动态NodeList与静态NodeList

    GitHub版本号: https://github.com/cncounter/translation/blob/master/tiemao_2014/NodeList/NodeList.md 副标题 ...

  6. 水晶易表 Xcelsius 2008 安装指南 完美支持office2010(亲手体验)

    Xcelsius2008水晶易表是一款很好用的软件.网上已经有破解方法,大家能够尝试一下这款经典软件了. 可是网上对于安装破解过程介绍的不详细或者纷乱,今天我汇总了全部的方法最终成功的安装上了,而且支 ...

  7. ubuntu终端方向键不能用(主机名不显示)问题的解决

    sudo gedit /etc/passwd 在/etc/passwd中改动该用户相应的shell:/bin/sh改为/bin/bash就可以解决该问题

  8. SSM框架理解(转)

    SSM框架理解 最近两星期一直在学JavaEE的MVC框架,因为之前学校开的JavaEE课程就一直学的吊儿郎当的,所以现在真正需要掌握就非常手忙脚乱,在此记录下这段时间学习的感悟,如有错误,希望大牛毫 ...

  9. vi 按了ctrl+s之后

    再windows不管是写程序.还是用Word写文件.已经习惯了按ctrl+s 保存代码. 在用vi的时候.常常无意中按了ctrl+s,结果就是如同终端死掉了一样. 这是由于ctrl+s 终止屏幕输出( ...

  10. Libgdx: 将Texturepacker打包的PNG图片还原成一张一张的单个的

    你是否发现用Texturepacker在打包压缩资源文件之后. 把原稿文件弄丢了,可是又要添加新的小png的时候,却无从下手了,本文就是博主在遇到这个问题后百度了非常多方法,可惜仅仅有plist格式的 ...