Click here to Skip to main content
15,908,618 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
C#
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;
            }
        }
Posted
Updated 5-Dec-16 1:33am
v2
Comments
Afzaal Ahmad Zeeshan 3-Dec-16 5:19am    
Why don't you use a print server and then let it handle this for you? You can then ping the server to know the status.
Vijai Anand.G 5-Dec-16 5:22am    
Tq..PrintServer class in system.printing namespace are not support to use within windows service or ASP.NET Applications.
F-ES Sitecore 5-Dec-16 4:10am    
The account your code runs under by default is very restrictive and has no access to the network so you'll struggle doing these kinds of things. You'll need to run your code under an account that has the desired access you need, google for how to change the account your code runs under, it depends on the version of IIS you are using.
Vijai Anand.G 5-Dec-16 5:18am    
Tq..You are speaking about impersonation.See I can able to print from my application to the network printer by means of impersonation. but my problem is that , I can't get the status from network printer.based on the status only i need to mark printed status in my database. How can I proceed?

1 solution

Hi,

please have a look with this below link,I think someone has already asked with the same.

How do I get the Printer status?[^]
 
Share this answer
 
Comments
Vijai Anand.G 6-Dec-16 0:37am    
Tq..I am Using Management Class in .Net Framework which is Common Information Model (CIM) Where I Can easily query the property values of WIN32 Printer using c#.but my problem is that capturing the status of printer like power off , paper out, paper jam, offline,etc., using c#..it returns improper result..
preety sunita 10-Dec-16 15:02pm    
Look at this links..

http://www.dreamincode.net/forums/topic/63969-how-to-get-extended-printer-status/
http://stackoverflow.com/questions/5001920/c-sharp-check-printer-status
http://stackoverflow.com/questions/296182/how-to-get-printer-info-in-net

I think, may its useful to get the solutions...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900