NetWork Printer Not getting Displayed in Vb.NET Services

Hi,

I work on VisualStudio 2005 and .NET framework 2.0

I have written the below code and compiled both as Windows application and windows services. The Results are different. I need to run my program as windows services. Please help.

I have added the Reference to Powerpack.Printing.

If I run the below code as windows application, all printers gets listed.

If I run the below code as windows services, NetWork Printers do not get listed.

Any help would be appreciated.

Imports Microsoft.VisualBasic.PowerPacks.Printing.Compatibility.VB6

PublicSub Listprinters()

Dim objPrinterAs Printer

Dim strtempAsString =""

Try

ForEach objPrinterIn Printers

strtemp = objPrinter.DeviceName

MessageBox.Show(strtemp)

Next

Catch exAs Exception

MessageBox.Show(ex.ToString())

EndTry

EndSub

[2153 byte] By [DhananjayaBk] at [2007-12-28]
# 1
well what are the differences in results? remember, the Windows Service runs under a different account than your user account always generally. So you may well experience different results if this is the case. Can you tell us what the differences are?
ahmedilyas at 2007-9-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Thanks for your reply. Well I have local printer connected to my system and also there are network printers. When I run as a windows application, I get all printers ( Local and network ). When I run as a windows services I can see only local printers but network printers do not get displayed.

We have registered our network printers by running at the command promt , \\servername\printer

Thanks once again

Regards

Dhananjaya

DhananjayaBk at 2007-9-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
how are you registering it? I mean do you do this when you log onto the computer? if so, have you tried to also do this on the network service account, or whatever account the Windows Service is running on, so it can also see it?
ahmedilyas at 2007-9-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

Thanks for your reply. I log into the system as network user and register the service as usual installutil <service name>.

I also checked the rights on the printer and all printers have been granted the rights. Not sure what more data i can provide.

Thanks

Regards

Dhananjaya

DhananjayaBk at 2007-9-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5

hmm ok. Do this (wouldnt recommend it)

open the services MMC Snapin (usually in the control panel > administrative tools > Services). Find your service. Double click on it and go to the Log on Tab. Next up change the Log on as to "This Account" and select your user account and enter password, click ok and restart the service. What happens now? does it work?

ahmedilyas at 2007-9-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6

HI,

I followed the above steps but still the network Printer is not getting displayed. When I run the same method as a windows application, network printer is getting displayed.

Can guide me to any good working examples of Listing down the all printers ( network & local) and printing them as a window services program.

Thanks

Regards

Dhananjaya

DhananjayaBk at 2007-9-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7

well one way is using WMI. add a reference to System.Management and import the System.Management and System.Management.Instrumentation namespaces. Then...



private sub DoShowPrinters()
Try
Dim searcher As New ManagementObjectSearcher( _
"root\CIMV2", "SELECT DeviceID FROM Win32_Printer")

For Each queryObj As ManagementObject in searcher.Get()
MessageBox.Show(queryObj("DeviceID").ToString())
Next
Catch err As ManagementException
MessageBox.Show("An error occurred while querying for WMI data: " & err.Message)
End Try

end sub

this is just one way of getting a list of printers installed on the computer.

ahmedilyas at 2007-9-3 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8

Thanks a Ton. I did run as a service and above code works. Just do not undersatnd what's the problem but looks like this a solution for my problem.

Thank You

Regards

Dhananjaya

DhananjayaBk at 2007-9-3 > top of Msdn Tech,Visual Basic,Visual Basic General...