简体   繁体   中英

Why my Windows Service can't execute an external application?

I have created a service to print PDF files. When I call FoxitReader trough a System.Process object, sending console commands to it, the service does nothing. I have readed about Session 0 isolation, but my service doesn't call a GUI application, it just execute a command in an executable file.

This is my code:

System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = "FoxitReader.exe";
            proc.StartInfo.Arguments = " /t " + '"' + nombreArchivo + '"' + " " + '"' + nombreImpresora + '"';
            EventLog.WriteEntry("InboundServicioImpresion", "Comando impresion:" + proc.StartInfo.FileName + proc.StartInfo.Arguments);
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.UseShellExecute = false;
            proc.Start();
            proc.WaitForExit();

NOTE: FoxitReader.exe is included in the service package; it's installed in the same folder as the service.

The services starts and write to the eventlog when the code says it has to do, and it doesn't crash or throw any exception (every method is controlled by try-catch). Also, it's capable to move files from one folder to another. The only thing that doesn't work is to print with foxit, adobe reader or sumatraPDF.

Any idea on why isn't it working?

Thanks in advance, Esteban.

You need the fully qualified name of the file instead of just the file name. Services have their default path set to C:\\Windows\\System32 (or applicable depending on your OS).

Change this line:

proc.StartInfo.FileName = "FoxitReader.exe";

to this (or just hardcode it if it is always a fixed path):

proc.StartInfo.FileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "FoxitReader.exe");

Are you running your service in the default, LocalSystem account? Perhaps it does not have access to the printer.

Try changing the service to run in and account that can print normally with FoxitReader and see if that works. Make the change on the "Log On" tab when configuring your service from the Windows Service Control Panel applet: http://www.powershellinside.com/kb/articles/powershellssh-useraccounts-1.jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM