Click here to Skip to main content
15,895,784 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have a problem in getting printer default property from WMI into service. The code I have used is as follows and it works perfectly as a console application. When I host it as windows service, the default printer is never returned. The value is always false.
static void Main(string[] args)
        {
            ConnectionOptions objConnection = new ConnectionOptions();
            
            string strMachineName = System.Environment.MachineName;
            ManagementScope objScope = new ManagementScope(@"\\" + strMachineName + @"\root\cimv2");//, objConnection);
            objScope.Connect();
            SelectQuery selectQuery = new SelectQuery();
            selectQuery.QueryString = "Select * from win32_Printer";
            ManagementObjectSearcher MOS = new ManagementObjectSearcher(objScope, selectQuery);
            ManagementObjectCollection MOC = MOS.Get();
            foreach (ManagementObject mo in MOC)
            {
                Console.WriteLine(MOC.Count.ToString());
                Console.WriteLine(mo["Name"].ToString().ToUpper());
                if ((bool)mo["Default"])
                {
                    Console.WriteLine("This is Default printer");
                }
            }
            Console.ReadLine();
        }


I am expecting an early reply for my project.

Surya
Posted
Updated 13-Jan-11 18:11pm
v2

You can expect an early reply all you want. You'll get one on our time, when we have time. We're all volunteers here, donating our time to help other people. Demanding we look at your problem first is just plain rude.

It doesn't work because the service is running as the Local System and so is your connection to WMI, not the user that's logged in. Even if you were to turn on "Allow interaction with Desktop", you still can't get the default printer for the user.

Services run even if noone is logged in, so how are you going to get the printers for someone who isn't there?
 
Share this answer
 
Sorry Dave for my urgency. I am running the service in locahost. But still it is not getting the default printer name.
 
Share this answer
 

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