Passing parameter to .exe file

futhon

Member
Joined
Feb 9, 2010
Messages
9
Programming Experience
Beginner
Hi,

I am currently still learning to use VB.Net feature. Need some help with the coding.

Currently i am trying to execute a .exe file where there will be passing parameters to run the script. For eg this how it works on the command prompt line:

C:\Documents and Settings\project1>textfile.exe file1.txt file2.txt file3.txt file4.txt

Above line will be able to run the textfile.exe script.

I am using VB .Net to create a User Interface where user will be able to use the 'OPEN CLICK' button to choose the files (which will display on a textbox) and click on a 'START' button to run the program.

I managed to find a code - process.start() feature, however it cant seem to work..

I'll show my code here and hopefully someone will be able to guide me thru my project. Thanks in advance.

VB.NET:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    Dim MyProg As String 
    Dim executable As String 

    executable = "C:\Documents and Settings\project1\textfile.exe"
    MyProg = TextBox1.Text
    Process.Start(x, MyProg)
 
it doesnt seem to work on my part. As my textbox1.text is displayed with the path name of all the text files like:

Textbox1.text = C:\Documents and Settings\project1\text\text1.txt C:\Documents and Settings\project1\text\text2.txt C:\Documents and Settings\project1\text\text3.txt C:\Documents and Settings\project1\text\text4.txt

how can i just pass the argument as text1.txt text2.txt text3.txt text4.txt .... ?
 
That data is pretty much no good. If the user wants you to pass just file names then they should enter just file names. Also, if they do enter a file path that contains spaces, they should wrap the path in double quotes. The data in the text box should be exactly what you'd type into a command prompt.

If you really do want to parse that data then you'd have to identify where the breaks between the paths are. That is possible if you can guarantee that every path is fully qualified. If so then you can use the colons as the reference points and work from there. Otherwise you're out of luck.
 
You say you have 'button to choose the files', probably this is a OpenFileDialog and you get the paths from FileNames property. To get only filename without full path you can use the IO.Path tools specifically IO.Path.GetFileName method. As mentioned, if an argument contains a space you also need to quote it. That textbox should probably not be editable, only for display purposes, and not used as command input - unless you want user to put any kind of text to your command. Something tells me you are going to need to set the working directory for the executable also, at least if it is going to find those unqualified file names, so if this is the case you need to use ProcessStartInfo class, set its properties, and Process.Start it.
 
Back
Top