2013-04-10 85 views
2

我正在设计一个应用程序,它需要识别打印机是否连接。我知道WMI列举连接的打印机的方法。但是,如果打印机在应用程序启动后连接,该怎么办?我们是否有任何事件或API?或者我必须经常使用WMI代码进行检查?打印机连接事件

为简单起见,我试过LocalPrintServer类是这样的:

PrintQueue printQueue = null; 

      LocalPrintServer localPrintServer = new LocalPrintServer(); 

      // Retrieving collection of local printer on user machine 
      PrintQueueCollection localPrinterCollection = 
       localPrintServer.GetPrintQueues(new[] { EnumeratedPrintQueueTypes.Local, EnumeratedPrintQueueTypes.Connections }); 

      System.Collections.IEnumerator localPrinterEnumerator = 
       localPrinterCollection.GetEnumerator(); 

      while(localPrinterEnumerator.MoveNext()) 
      { 
       // Get PrintQueue from first available printer 
       printQueue = (PrintQueue)localPrinterEnumerator.Current;      

       if (!printQueue.IsOffline) 
       { 

        string s = "Printer found " + printQueue.FullName.ToString(); 
        listBox1.Items.Add(s); 
        bDeviceFound = true; 
        button1.Enabled = bDeviceFound; 


       } 

你知道它会显示在其上运行的应用程序启动时安装的打印机。问题是无法确定应用程序启动后是否连接了打印机。如果我们有任何事件发生,我可以调用这个方法来重新枚举打印机,除非我是为了线程或while循环?

谢谢

+0

你有没有试过_anything_?显示你的工作.. – 2013-04-10 06:43:50

+0

请看我的工作 – Zigma 2013-04-10 06:50:47

+0

我不能直接帮助你,但尝试谷歌的任何博客条目/代码邓肯爱德华琼斯。他似乎是适当的.net打印大师。 – svenv 2013-04-10 07:30:45

回答

1

解决了这个问题,通过设置定时器来提取细节。