C# - LPR Command to Print PDF Files

2019-07-27 03:05发布

I am trying to run an LPR command to print a PDF. The code I'm using is being executed from a button click in a windows forms application.

Code:

var command = @"lpr –S 192.168.1.245 –P DAILY C:\Test.pdf";
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/c " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardError = true;
procStartInfo.CreateNoWindow = true;

// start process
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();

proc.WaitForExit();

// read process output
string cmdError = proc.StandardError.ReadToEnd();
string cmdOutput = proc.StandardOutput.ReadToEnd();

The program is running as an x64 program, so it can find lpr program in the C:\Windows\System32 folder.

When the code executes the error string is empty and the output string contains the following (same output as if the command run had been lpr /?)

Output:

Sends a print job to a network printer

Usage: lpr -S server -P printer [-C class] [-J job] [-o option] [-x] [-d] filename

Options: -S server Name or ipaddress of the host providing lpd service -P printer Name of the print queue -C class Job classification for use on the burst page -J job Job name to print on the burst page -o option Indicates type of the file (by default assumes a text file) -x Compatibility with SunOS 4.1.x and prior -d Send data file first

If I copy and paste the command exactly as it appears in the code and paste it into a command window, even if it's the SAME command window the application opened, it works fine.

Does anyone have any insight into why this would be happening? Thanks in advance!

1条回答
劳资没心,怎么记你
2楼-- · 2019-07-27 03:14

I was able to figure it out. If it helps anyone else, the code that is working is

var command = @"lpr -S 192.168.1.245 -P ""DAILY"" ""C:\Test.pdf""";
ProcessStartInfo procStartInfo = new ProcessStartInfo("cmd", "/C " + command);
procStartInfo.RedirectStandardOutput = true;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardError = true;
procStartInfo.CreateNoWindow = true;

// start process
Process proc = new Process();
proc.StartInfo = procStartInfo;
proc.Start();

proc.WaitForExit();

// read process output
string cmdError = proc.StandardError.ReadToEnd();
string cmdOutput = proc.StandardOutput.ReadToEnd();

Notice that the Queue Name and Filename are surrounded by quotes. My other issue was that I'd originally copied the command from an email so the dash was wrong, I had to delete and manually type the dashes in the command for it to be recognized. If you look really close in the original question, you can see the dash is slightly longer.

For information on making sure the lpr command is available, especially if you're running a 32 bit app on a 64 bit machine, see the following references.

Reference 1: lpr command not working from my C# program in Win 7

Reference 2: http://www.tomshardware.com/forum/240019-44-error-windows

查看更多
登录 后发表回答