user3276838
user3276838

Reputation: 9

Passing arguments in Process.Start()

I want to pass the name of a file as an argument in Process.Start("", ""). However, my parameters don't seem to work.

Here is my code:

Public Class Form1

Public Sub Button1_click(sender As Object, e As EventArgs) Handles btnClick.Click

    Dim myFile0 As String = "C:\Users\Desktop\1.pdf"
    Dim myFile2 As String = "C:\Users\Desktop\1s.pdf"

    Process.Start("cmd.exe", "/k pdftk" & myFile & "output" & myFile2 & "owner_pw password")

That does not work, but if I use the file path instead of myFile0 or myFile2 it works fine. I need to be able to use the variables.

Any inputs why it doesn`t work. I am new to vb.net

Thanks!

Upvotes: 0

Views: 5347

Answers (2)

Hans Passant
Hans Passant

Reputation: 941525

You are forgetting the spaces before and after the file name. Double quotes are also strongly recommended to avoid trouble if the path name contains spaces. This is always easier to get right when you use composite formatting. Fix:

Dim args = String.Format("/k pdftk {0}{1}{0} output {0}{2}{0} owner_pw password", _
               """", myFile, MyFile2)
Process.Start("cmd.exe",  args)

Upvotes: 2

NotJehov
NotJehov

Reputation: 43

If just a filepath is specified in the parameters, Process.Start will attempt to launch the file with the default file handler (such as Adobe for PDF files). If you want to launch a specific program with arguments, you should pass the executable path to the program that is handling the .pdf file, with the command line arguments being in the second parameter.

Process.Start(PathToPDFHandler, "/k pdftk" & myFile & "output" & myFile2 & "owner_pw password")

Something like that.

Upvotes: 1

Related Questions