如何用C#判断打印机工作状态
方法一:
using System.DirectoryServices;
using ActiveDs;(Com 组键里的 Active DS Type Library 目录Windos\system32\activeds.tlb)
//Printer status flags - see SDK docs for status values (IADsPrintQueueOperations) 打印机状态
[Flags]
private enum PrinterStatus
{
Paused = 1,
DeletePending = 2,
Error = 3,
PaperJam = 4,
PaperOut = 5
}
private void GetPrinterStatus1()
{
try
{
this.Cursor = Cursors.WaitCursor;
DirectoryEntry lPrinter = new DirectoryEntry("WinNT://192.168.1.77/Adobe PDF", null, null, AuthenticationTypes.ServerBind);
//Printer path
txtState.Text += Environment.NewLine + "Printer path:" + lPrinter.Path;
System.DirectoryServices.PropertyCollection lPropertyColl = lPrinter.Properties;
foreach (string pcName in lPropertyColl.PropertyNames)
{
txtState.Text += Environment.NewLine + pcName + ":\t" + (lPropertyColl[pcName])[0].ToString();
}
txtState.Text += Environment.NewLine + "---------------------------------";
// Use the PrintqueueOperations interface to contol the printer Queue
IADsPrintQueueOperations lPrintQueue = lPrinter.NativeObject as IADsPrintQueueOperations;
if (lPrintQueue != null)
{
if (lPrintQueue.Status == (int)PrinterStatus.Paused) // If paused resume else pause printer
{
lPrintQueue.Resume();
}
else
{
lPrintQueue.Pause();
}
// Use the IADsPrintJob to control individual printjobs
foreach (IADsPrintJob pJob in lPrintQueue.PrintJobs())
{
txtState.Text += Environment.NewLine + pJob.Description + pJob.Name;
IADsPrintJobOperations pjo = pJob as IADsPrintJobOperations;
txtState.Text += Environment.NewLine + pjo.Name;
// Use IADsPrintJob.Name as arg. to remove the job from the queue
//删除打印列表里的庸余打印任务
//lPrintQueue.PrintJobs().Remove(pJob.Name);
}
}
lPrinter.Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
this.Cursor = Cursors.Default;
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。