嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 3 元微信扫码支付:3 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
C#实现打印机监控
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System;
using System.Collections.Generic;
using System.Text;
using System.Printing;
using System.Collections;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.Text == string.Empty)
return;
PrinterStatus ret = 0;
string path = @"win32_printer.DeviceId='" @comboBox1.Text "'";
ManagementObject printer = new ManagementObject(path);
printer.Get();
PropertyDataCollection.PropertyDataEnumerator pde = printer.Properties.GetEnumerator();
string strRow="";
while (pde.MoveNext())
{
strRow = pde.Current.Name " : " pde.Current.Value "\r\n";
//显示的是 属性名 : 属性值 的形式
}
string aTmp = printer.Properties["WorkOffline"].Value.ToString();
if (aTmp == "True")
{
label2.Text = "打印机未联机";
txtStatus.Text = textBox2.Text = "";
}
else
{
label2.Text = "打印机连接正常";
ret = (PrinterStatus)Convert.ToInt32(printer.Properties["PrinterStatus"].Value);
txtStatus.Text = ret.ToString();
string strRet = "";
switch (Convert.ToInt32(printer.Properties["PrinterState"].Value))
{
case 0:
strRet = "准备就绪(Ready)";
break;
case 1:
strRet = "暂停(Paused)";
break;
case 2:
strRet = "错误(Printer Error)";
break;
case 4:
strRet = "正在删除(Pending Deletion)";
break;
case 8:
strRet = "塞纸(Paper Jam)";
break;
case 16:
strRet = "打印纸用完(Paper Out)";
break;
case 32:
strRet = "手工送纸(Manual Feed)";
break;
case 64:
strRet = "纸张问题(Page Problem)";
break;
case 128:
strRet = "脱机(Off Line)";
break;
case 256:
strRet = "正在输入,输出(I/O Active)";
break;
case 512:
strRet = "忙(Busy)";
break;
case 1024:
strRet = "正在打印(Printing)";
break;
case 2048:
strRet = "输出口已满(Output Bin Full)";
break;
case 4096:
strRet = "不可用(Not Available)";
break;
case 8192:
strRet = "等待(Waiting)";
break;
case 16384:
strRet = "正在处理(Processing)";
break;
case 32768:
strRet = "初始化(Initializing)";
break;
case 65536:
strRet = "热机中(Warming Up)";
break;
case 131072:
strRet = "墨粉不足(Toner Low)";
break;
case 262144:
strRet = "无墨粉(No Toner)";
break;
case 524288:
strRet = "当前页无法打印(Page Punt)";
break;
case 1048576:
strRet = "需要用户干预(User Intervention)";
break;
case 2097152:
strRet = "内存溢出(Out of Memory)";
break;
case 4194304:
strRet = "被打开(Printer Door Open)";
break;
case 536870912:
strRet = "等待(Waiting)";
break;
default:
strRet = "未知状态(Unknown Status)";
break;
}
textBox2.Text = strRet;
}
textBox1.Text = strRow;
}
internal enum PrinterStatu
{
PRINTER_STATUS_BUSY = 0x00000200,
PRINTER_STATUS_DOOR_OPEN = 0x00400000,
PRINTER_STATUS_ERROR = 0x00000002,
PRINTER_STATUS_INITIALIZING = 0x00008000,
PRINTER_STATUS_IO_ACTIVE = 0x00000100,
PRINTER_STATUS_MANUAL_FEED = 0x00000020,
PRINTER_STATUS_NO_TONER = 0x00040000,
PRINTER_STATUS_NOT_AVAILABLE = 0x00001000,
PRINTER_STATUS_OFFLINE = 0x00000080,
PRINTER_STATUS_OUT_OF_MEMORY = 0x00200000,
PRINTER_STATUS_OUTPUT_BIN_FULL = 0x00000800,
PRINTER_STATUS_PAGE_PUNT = 0x00080000,
PRINTER_STATUS_PAPER_JAM = 0x00000008,
PRINTER_STATUS_PAPER_OUT = 0x00000010,
PRINTER_STATUS_PAPER_PROBLEM = 0x00000040,
PRINTER_STATUS_PAUSED = 0x00000001,
PRINTER_STATUS_PENDING_DELETION = 0x00000004,
PRINTER_STATUS_PRINTING = 0x00000400,
PRINTER_STATUS_PROCESSING = 0x00004000,
PRINTER_STATUS_TONER_LOW = 0x00020000,
PRINTER_STATUS_USER_INTERVENTION = 0x00100000,
PRINTER_STATUS_WAITING = 0x20000000,
PRINTER_STATUS_WARMING_UP = 0x00010000
}
enum PrinterStatus
{
其他状态 = 1,
未知,
空闲,
正在打印,
预热,
停止打印,
打印中,
离线
}
/// <summary>
/// 打印机属性转换
/// </summary>
/// <param name="PrinterDevice">打印机名(DeviceID)</param>
/// <param name="iCount">当前任务数</param>
/// <returns>打印机状态名字</returns>
private PrinterStatus GetPrinterStat(string PrinterDevice, ref int iCount)
{
PrinterStatus ret = 0;
string path = @"win32_printer.DeviceId='" @comboBox1.Text "'";
ManagementObject printer = new ManagementObject(path);
printer.Get();
ret = (PrinterStatus)Convert.ToInt32(printer.Properties["PrinterStatus"].Value);
iCount = Convert.ToInt32(printer.Properties["JobCountSinceLastReset"].Value);
return ret;
}
private void Form1_Load(object sender, System.EventArgs e)
{
foreach (string iprt in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
this.comboBox1.Items.Add(iprt);
}
timer1.Interval = 500;
timer1.Start();
}
private void timer1_Tick(object sender, System.EventArgs e)
{
button1.PerformClick();
}
private void button2_Click(object sender, EventArgs e)
{
PrintQueue pq = LocalPrintServer.GetDefaultPrintQueue();
switch (pq.QueueStatus)
{
//处于节能状态
case PrintQueueStatus.PowerSave:
break;
//处于错误状态
case PrintQueueStatus.ServerUnknown:
break;
//打印机上的门已打开
case PrintQueueStatus.DoorOpen:
break;
//打印机无可用内存
case PrintQueueStatus.OutOfMemory:
break;
case PrintQueueStatus.UserIntervention:
break;
case PrintQueueStatus.PagePunt:
break;
//打印机墨粉已用完
case PrintQueueStatus.NoToner:
break;
case PrintQueueStatus.TonerLow:
break;
case PrintQueueStatus.WarmingUp:
break;
case PrintQueueStatus.Initializing:
break;
case PrintQueueStatus.Processing:
break;
case PrintQueueStatus.Waiting:
break;
case PrintQueueStatus.NotAvailable:
break;
case PrintQueueStatus.OutputBinFull:
break;
case PrintQueueStatus.Busy:
break;
case PrintQueueStatus.IOActive:
break;
case PrintQueueStatus.Offline:
break;
case PrintQueueStatus.PaperProblem:
break;
case PrintQueueStatus.ManualFeed:
break;
//打印机缺纸
case PrintQueueStatus.PaperOut:
break;
//打印机卡纸
case PrintQueueStatus.PaperJam:
break;
case PrintQueueStatus.PendingDeletion:
break;
case PrintQueueStatus.Paused:
break;
case PrintQueueStatus.None:
break;
case PrintQueueStatus.Printing:
break;
case PrintQueueStatus.Error:
break;
default:
break;
}
}
}
}