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 循环?

谢谢

4

1 回答 1

1

通过设置计时器来提取详细信息解决了该问题。

于 2013-05-10T05:49:06.470 回答