【问题标题】:Talking to a printer与打印机交谈
【发布时间】:2015-04-04 03:36:37
【问题描述】:

有没有办法编写一些可以与打印机“对话”的代码以获取有关其状态的一些基本信息?我真正感兴趣的是找出它是否已用完纸或卡纸——这种性质的事情。我应该为这类东西使用 System.Management 库吗?

PS - 知道如何获取已在特定 PC 上设置的所有打印机也很方便。你会怎么做呢?

【问题讨论】:

  • 戴上笑话帽我父亲是个打印机,我每天都和他说话。
  • 坚持编程 Olafur ! :)

标签: c# printing


【解决方案1】:

使用 System.Management 从打印机获取信息相对容易。

    //Declare WMI Variables
    ManagementObject MgmtObject;
    ManagementObjectCollection MgmtCollection;
    ManagementObjectSearcher MgmtSearcher;

    //Perform the search for printers and return the listing as a collection
    MgmtSearcher = new ManagementObjectSearcher("Select * from Win32_Printer");
    MgmtCollection = MgmtSearcher.Get();

    foreach (ManagementObject objWMI in MgmtCollection)
    {
       //Do whatever action you want with the Printer
    }

查看http://msdn.microsoft.com/en-us/library/aa394363.aspx 了解 Win32_Printer 的方法和属性。对于您的问题:

//Test whether a Win32_Printer is out of paper or jammed
int state = Int32.Parse(objWMI["PrinterState"]);
if (state == 4) {
   //Paper Jam
} else if (state == 5) {
   //Paper Out
}

【讨论】:

    【解决方案2】:

    你也可以使用LINQ to WMI api

    【讨论】:

      猜你喜欢
      • 2011-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-05
      • 1970-01-01
      • 2023-03-17
      • 2021-12-07
      • 2011-12-31
      相关资源
      最近更新 更多