4
Answers

Command Line Print embedded in C# not working

Abdullah Arshad

Abdullah Arshad

12y
4.6k
1
HI There

I'm using FOXIT READER to print a PDF to a network.

When I run the following in command line at DOS prompt

C:\Program Files (x86)\Foxit Software\Foxit Reader>"foxit reader.exe" /t "c:\po222.pdf" "\\CCS-BAR-001\NRG SP 4100N PS 

It works fine

 
However, when I embed the command line into Visual Studio C# using the Process Class, it fails to recognize the printer name.

 

I think this has something to do with there being both a double forward slash and single forward slash in the printer name which C# is failing to recognise even though I am using @ in front of the string to represent the literal string.

 

My C# code is :

 

            string testFile = "c:\\po222.pdf";

            string testPrinter = @"\\CCS-BAR-001\NRG SP 4100N PS";

 

            Process pdfProcess = new Process();

            pdfProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

            pdfProcess.StartInfo.FileName = @"C:\Program Files (x86)\Foxit Software\Foxit Reader\Foxit Reader.exe";

            pdfProcess.StartInfo.Arguments = string.Format("/t {0} {1}", testFile, testPrinter);

            pdfProcess.StartInfo.CreateNoWindow = false;

            pdfProcess.Start();

            pdfProcess.CloseMainWindow();

I can't see what I'm doing wrong. Can you spot anything out of place.

 

Any help would be gratefully received.
 

Kind regards

 

Abdullah

Answers (4)
1
Vulpes

Vulpes

NA 98.3k 1.5m 12y
Yes, that should be possible.

You could check whether there were any embedded spaces in the printer name or not but it's easiest to always place the name in quotes. This will not do any harm even if the quotes are not needed.

Suppose the printer is obtained from the database and placed in a variable called printerName. Then you can redefine the testPrinter variable as follows:

string testPrinter  = @"""" + printerName + @""""; 

1
Vulpes

Vulpes

NA 98.3k 1.5m 12y
I think the problem is that you need to embed some quotes around any element which contains spaces, more or less as you did when running it from the command line. 

So I'd try:

            string testFile = "c:\\po222.pdf";
            string testPrinter = @"""\\CCS-BAR-001\NRG SP 4100N PS""";
 
            Process pdfProcess = new Process();
            pdfProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
            pdfProcess.StartInfo.FileName = @"""C:\Program Files (x86)\Foxit Software\Foxit Reader\Foxit Reader.exe""";
            pdfProcess.StartInfo.Arguments = string.Format("/t {0} {1}", testFile, testPrinter);
            pdfProcess.StartInfo.CreateNoWindow = false;
            pdfProcess.Start();
            pdfProcess.CloseMainWindow();
0
Abdullah Arshad

Abdullah Arshad

NA 6 11.7k 12y
That worked an absolute treat.

I raise my hat to you sir !!!!

Many many thanks !!!

Abdullah
0
Abdullah Arshad

Abdullah Arshad

NA 6 11.7k 12y
Thank you kindly Vulpes.....it worked.

Sorry could I ask you one more thing regarding this.

Would I able to achieve attaching @ and quotes around quotes on a printer name (i.e. @"""\\CCS-BAR-001\NRG SP 4100N PS""" ) if that printer name was determned from a data table.

Kind regards.
 
Abdullah