Problem running a command when the path has spaces in its name.
-
Backgroud: I'm trying to automate the printing of PDF files. I found that by invoking
acrord32.exe /N /T pdfFile printerName
on the command line you can print PDFs silently. Great. I'm trying to automate this in VB via
Dim p As Process = New Process()
Dim pi As ProcessStartInfo = New ProcessStartInfo()
Dim command As String = Chr(34) & "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\acrord32.exe" & Chr(34) & " /N /T "Try pi.Arguments = String.Format("{0} {1} {2} {3}{4}{5}", " /C ", command, sFilename, Chr(34), sPrinter, Chr(34)) pi.UseShellExecute = False pi.RedirectStandardError = True pi.RedirectStandardOutput = True pi.CreateNoWindow = True pi.WindowStyle = ProcessWindowStyle.Normal pi.FileName = "cmd.exe" p.StartInfo = pi p.Start() Do Until p.HasExited : Loop outp = p.StandardOutput.ReadToEnd Debug.Print(outp) outp = p.StandardError.ReadToEnd Debug.Print(outp) Catch ex As Exception Debug.Print(ex.Message) End Try
The error message I get from StdErr is
Quote:
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
I've tried putting quotes around the command string, but it just doesn't work. Turning to the community for guidance. :java:
-
Backgroud: I'm trying to automate the printing of PDF files. I found that by invoking
acrord32.exe /N /T pdfFile printerName
on the command line you can print PDFs silently. Great. I'm trying to automate this in VB via
Dim p As Process = New Process()
Dim pi As ProcessStartInfo = New ProcessStartInfo()
Dim command As String = Chr(34) & "C:\Program Files (x86)\Adobe\Reader 11.0\Reader\acrord32.exe" & Chr(34) & " /N /T "Try pi.Arguments = String.Format("{0} {1} {2} {3}{4}{5}", " /C ", command, sFilename, Chr(34), sPrinter, Chr(34)) pi.UseShellExecute = False pi.RedirectStandardError = True pi.RedirectStandardOutput = True pi.CreateNoWindow = True pi.WindowStyle = ProcessWindowStyle.Normal pi.FileName = "cmd.exe" p.StartInfo = pi p.Start() Do Until p.HasExited : Loop outp = p.StandardOutput.ReadToEnd Debug.Print(outp) outp = p.StandardError.ReadToEnd Debug.Print(outp) Catch ex As Exception Debug.Print(ex.Message) End Try
The error message I get from StdErr is
Quote:
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
I've tried putting quotes around the command string, but it just doesn't work. Turning to the community for guidance. :java:
You have to separate the command line options from the command you're trying to execute. For your "filename", you've got:
"C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\acrord32.exe" /N /T
That doesn't work. Your filename and arguments must be specified separately:
Dim command As String = """C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\acrord32.exe""" Dim arguments As String = String.Format("/n /t ""{0}"" ""{1}"" ""{2}"" ""{3}""", pdfFilePath, printerName, driverName, portName) Dim process As New Process Dim pi As New ProcessStartInfo pi.Filename = command pi.Arguments = arguments process.StartInfo = pi process.Start
Yes, the double quotes are there for a reason. It's shorter to type "" than it is to type
Chr$(34) &
.Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave Kreskowiak -
You have to separate the command line options from the command you're trying to execute. For your "filename", you've got:
"C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\acrord32.exe" /N /T
That doesn't work. Your filename and arguments must be specified separately:
Dim command As String = """C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\acrord32.exe""" Dim arguments As String = String.Format("/n /t ""{0}"" ""{1}"" ""{2}"" ""{3}""", pdfFilePath, printerName, driverName, portName) Dim process As New Process Dim pi As New ProcessStartInfo pi.Filename = command pi.Arguments = arguments process.StartInfo = pi process.Start
Yes, the double quotes are there for a reason. It's shorter to type "" than it is to type
Chr$(34) &
.Asking questions is a skill CodeProject Forum Guidelines Google: C# How to debug code Seriously, go read these articles.
Dave KreskowiakYour suggestion worked perfectly. I was over-thinking the problem. I was trying to run cmd.exe and pass the acrord32.exe as a parameter. Your solution of running acrord32.exe directly worked. Thank you.