winform 如何获取局域网中所有打印机的状态? 不是直连  就是通过网络共享的  能否获得该打印机的所有资源?

解决方案 »

  1.   

    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)
    {    string name = objWMI["Name"].ToString().ToLower();    if (name.Equals(printerName.ToLower()))
        {        int state = Int32.Parse(objWMI["ExtendedPrinterStatus"].ToString());
            if ((state == 1) || //Other
            (state == 2) || //Unknown
            (state == 7) || //Offline
            (state == 9) || //error
            (state == 11) //Not Available
            )
            {
            throw new ApplicationException("hope you are finally offline");
            }        state = Int32.Parse(objWMI["DetectedErrorState"].ToString());
            if (state != 2) //No error
            {
            throw new ApplicationException("hope you are finally offline");
            }    }}