0

在我的项目方法中,水晶报表的 PrintToPrint 工作正常,但是在打印机未连接到机器时安装软件后,此方法使我的软件进程挂起。谁能帮我解决这个问题?

4

1 回答 1

1

我的解决方案是在调用打开水晶报表之前编写一个函数来确认打印机在线。

using System.Management;

public bool IsPrinterReady(string printerName)
        {

        bool bprinterOnline = false;       

        ManagementScope scope = new ManagementScope(@"\root\cimv2");
        scope.Connect();

        // Select Printers from WMI Object Collections
        ManagementObjectSearcher printerSearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Printer");

        foreach (ManagementObject printer in printerSearcher.Get())
        {

            if (string.IsNullOrEmpty(printer["Name"].ToString()))
            {
                if (printer["Name"].ToString().ToLower().Equals(printerName.ToLower()))
                {

                    switch (printer["WorkOffline"].ToString().ToLower())
                    {
                        case "true":
                            bprinterOnline= true;
                            break;
                        case "false": 
                            bprinterOnline= false;
                            break;
                        default:
                            bprinterOnline= false;
                            break;
                    }
                    break;                     
                }
            }
        }
        return bprinterOnline;
    }
于 2012-09-28T13:00:29.623 回答