如何在ASP.NET C#中获取网络打印机的状态

问题描述:





我的要求是从我的ASP.NET应用程序获取网络打印机状态,并根据我需要更新数据库详细信息过程。



在我的情况下,对于网络打印机它总是返回Empty.I不认为我正在以正确的方式进行。任何人请告诉我正确的方法来实现这一点。



任何帮助将不胜感激。



我尝试过:



我的代码是这样的

Hi,

My Requirement is to get the Network Printer status from my ASP.NET Application and based on that I need to update the database details for the next process.

In my case, For Network Printer It is always return Empty.I don't think that i am going on the correct way. Anyone please tell me the correct way to achieve this.

Any help would be greatly appreciated.

What I have tried:

My Code is like this

private string GetPrintJobStatus()
        {
            ObjectQuery wmiQuery = null;
            ManagementObjectSearcher objManagementobjectSearcher = null;
            ManagementObjectCollection objManagementObjectCollection = null;

            string Name = ""; // wmi print job is combination of "printerName , job number"
            string[] namesplit = null;
            string l_printerName = "";
            string l_jobNumber = "";
            string l_jobStatus = "";
            string l_status = "";

            string l_ReturnStatus = "";
            try
            {
                wmiQuery = new ObjectQuery("Select * from Win32_PrintJob");
                objManagementobjectSearcher = new ManagementObjectSearcher(wmiQuery);
                objManagementObjectCollection = objManagementobjectSearcher.Get();
                foreach (ManagementObject printJob in objManagementObjectCollection)
                {
                    Name = Convert.ToString(printJob["Name"]);
                    namesplit = Name.Split(',');
                    l_printerName = namesplit[0];
                    l_jobNumber = namesplit[1];
                    if (Convert.ToString(Session["printername"]) != "")
                    {
                        if (l_printerName == Convert.ToString(Session["printername"]))
                        {
                            l_status = Convert.ToString(printJob["Status"]);

                            if (l_status.ToUpper().Equals("OK"))
                            {
                                l_jobStatus = Convert.ToString(printJob["JobStatus"]);

                                switch (l_jobStatus)
                                {
                                    case PrintJobStatus.Blocked:
                                        l_ReturnStatus = "Printer Job is Blocked";
                                        break;

                                    case PrintJobStatus.Completed:
                                        l_ReturnStatus = "Printing is Completed";
                                        break;

                                    case PrintJobStatus.Deleted:
                                        l_ReturnStatus = "Printer Job is Deleted";
                                        break;

                                    case PrintJobStatus.Deleting:
                                        l_ReturnStatus = "Printer Job is Deleting";
                                        break;

                                    case PrintJobStatus.Error:
                                        l_ReturnStatus = "Printer is on Error";
                                        break;

                                    case PrintJobStatus.Offline:
                                        l_ReturnStatus = "Printer is on Offline";
                                        break;

                                    case PrintJobStatus.PaperOut:
                                        l_ReturnStatus = "Printer is on PaperOut";
                                        break;

                                    case PrintJobStatus.Paused:
                                        l_ReturnStatus = "Printer Job is Paused";
                                        break;

                                    case PrintJobStatus.Printed:
                                        l_ReturnStatus = "Printing is Completed";
                                        break;

                                    case PrintJobStatus.Printing:
                                        l_ReturnStatus = "Printing is Completed";
                                        break;

                                    case PrintJobStatus.Spooling:
                                        l_ReturnStatus = "Printing is Completed";
                                        break;

                                    case PrintJobStatus.UserIntervention:
                                        l_ReturnStatus = "Printing is on User Intervention";
                                        break;
                                }
                            }
                            else
                            {
                                l_ReturnStatus = "Can't Print.Error in Printer,Contact your Network Administrator.";
                            }
                        }
                       
                    }
                    else
                    {
                        l_ReturnStatus = "No Default Printer is Selected.";
                    }
                   
                }

                return l_ReturnStatus;

            }
            catch (Exception)
            {
                throw;
            }
        }





请看下面这个链接,我想有人已经问过同样的问题。



如何获取打印机状态? [ ^ ]