9

I have a Windows Service that needs to start a process to send a file to the printer (I found that solution there https://stackoverflow.com/a/4875755/1228738) . I do this using the Process.Start(). My problem is that nothing happens.

The service is actually installed on my developer machine (win7, x64). I tried installing it as LOCAL SYSTEM, NETWORK SERVICE, LOCAL SERVICE with the same result every time.

I tried those way of starting my process :

Process p = new  Process();
p.StartInfo.FileName = "C:\\Program Files (x86)\\Foxit Software\\Foxit Reader\\Foxit Reader.exe";
p.StartInfo.Arguments = "-p myFile.pdf";
p.Start();

and

Process.Start("C:\\Program Files (x86)\\Foxit Software\\Foxit Reader\\Foxit Reader.exe", "-p myFile.pdf");

and also

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "C:\\Program Files (x86)\\Foxit Software\\Foxit Reader\\Foxit Reader.exe";
startInfo.Arguments = "-p myFile.pdf";

Process.Start(startInfo);

When I execute the same code in a winform application, everything works fine, the file is sent to the printer. But in the Windows Service, nothing happens.

I saw that post https://stackoverflow.com/a/6271309/1228738, which explains why I would not see the UI, that's fine I don't have any UI anyway. But as said in the comment section, a process with no user input should be OK. The process that I start don't need any user input.

The only thing I can think of right now, is that because of session isolation (https://stackoverflow.com/a/5063750/1228738), the service can't find any installed printers... Can that be the case ? If so, any suggestion how to work around that ? And if not, any idea of what's wrong ?

Thanks!

EDIT #1

I tried running the service with my user account, and it's working, so I guess my fears are confirmed... the users LOCAL SYSTEM and NETWORK SERVICE have no installed printers.

So I'll refine my question a little bit. Is there a way for those account to access printers installed on the computer ?

EDIT #2

We finally decided that a user will be created for running that service and in that user accounts we'll install the printer on which to print.

I guess this question can be closed now. Thank you all for your help.

Community
  • 1
  • 1
Fid
  • 568
  • 5
  • 13
  • It could be because the application is attempting to show the print dialog but the session is non-interactive (check the application event log) or it could be, as you suggest, because the user the service runs as has no printers configured. You could log in as the user the service runs as (if possible) and configure printers. – Paul Ruane Feb 28 '12 at 20:28
  • When I do the exact same code in a winform application, the file is sent to the printer without the print dialog, so I would expect the same behavior from the service. I'll try something similar to what you suggest, and run the service with my user account, and see if it's working that way. Thanks – Fid Feb 28 '12 at 20:31
  • also, where is myFile.pdf located? Does the service have access to that path? – roymustang86 Feb 28 '12 at 21:31
  • Yes, I just tested with the full control permissions for the account network service on the folder where the file is located with no more success. Thanks for the suggestion. – Fid Feb 28 '12 at 21:47
  • No. This cannot be done with a Windows Service. – Cody Gray - on strike Feb 29 '12 at 03:38
  • I'm not very well comfortable with this answer but please try this. The StartInfo you have given is consisted with only filename and arguments. Try giving user name and a password you want to run the printing application in. In default I don't think it's possible in local system account. Try your local admin account. – Pulathisi Bandara Nov 11 '15 at 04:13

3 Answers3

3

I had this issue too, this trick solved it

Go to services ---> Double click the required service ---> proceed to logon tab

Supply the Log-in credentials from which printer was installed.

Run your service, then check the printer queue.

Reason: Local system account does not have those printer installed !

See screen shot below.enter image description here

Edwin O.
  • 4,998
  • 41
  • 44
0

The solution here is tho share your local printer and call Foxit with

-/t yourfile.pdf \\localhost\YourSharedPrinter

That way your service does not need an UserProfile and no DefaultPrinter.

0

Check out this MSDN Page: http://support.microsoft.com/kb/324565

According to this page, you cannot print from ASP.NET pages or Windows services using .NET.

Icemanind
  • 47,519
  • 50
  • 171
  • 296
  • 2
    Thanks for the link, but I don't think it applies to my situation. I'm not using the .NET Framework Printing classes for the printing. I'm just starting an .exe which take an argument telling him to print the document. – Fid Feb 29 '12 at 14:03