Executing an External Command

jlwilson

Member
Joined
Jun 23, 2005
Messages
11
Location
Harrison, AR
Programming Experience
Beginner
I'm a beginner go easy on me! I've only completed VB.NET I in college. I am trying to write an application that will run an external command such as the following:

start "Pinging Host" 127.0.0.1 -t

The above is just an example (not really what I'm trying to ping). I can't figure out how to make my button execute the command. I performed a messagebox.show to display my command string to ensure that it was being read correctly. Then I tried to execute it by using the following code in my button:

Simplified Code below...
VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strCommandString [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2]strCommandString = [/SIZE][SIZE=2][COLOR=#800000]"start ""Pinging 127.0.0.1"" ping 127.0.0.1 -t"
[/COLOR][/SIZE][SIZE=2]MessageBox.Show(strCommandString, [/SIZE][SIZE=2][COLOR=#800000]"Output of strCommandString"[/COLOR][/SIZE][SIZE=2], MessageBoxButtons.OK, MessageBoxIcon.Information)
Shell(strCommandString)
[/SIZE]

I don't know how to properly execute the string. I tried using Shell. Should I be doing this some other way?

The basic reason I'm even using "Start" is because I need the ping window to show which location is being pinged because often we have several windows open and it can get confusing without the window label.

Anyone have any ideas on what I'm doing wrong?
 
Process.Start()

I wanted to mention that I've also tried to use Process.Start() and cannot get that to do it either. I continue to get a message stating something about a filename exception error.

VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strCommandString [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2]strCommandString = [/SIZE][SIZE=2][COLOR=#800000]"start ""Pinging 127.0.0.1"" ping 127.0.0.1 -t"
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#008000]'MessageBox.Show(strCommandString, "Output of strCommandString", MessageBoxButtons.OK, MessageBoxIcon.Information)
[/COLOR][/SIZE][SIZE=2]System.IO.Directory.SetCurrentDirectory([/SIZE][SIZE=2][COLOR=#800000]"c:\windows\system32\"[/COLOR][/SIZE][SIZE=2])
'Process.Start([/SIZE][SIZE=2][COLOR=#800000]"c:\windows\system32\ping.exe 127.0.0.1 -t"[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2]Process.Start(strCommandString)
[/SIZE]

Neither of the Process.Start() formats I'm using works either. I'm at a loss. I've confirmed that the command executes manually from a command prompt and confirmed that the ping.exe file is in the folder specified but it continues to error out.
 
change this line:
VB.NET:
[FONT=monospace]Process.Start("c:\windows\system32\ping.exe 127.0.0.1 -t")[/FONT]

to:
VB.NET:
[FONT=monospace]Process.Start("c:\windows\system32\ping.exe", "127.0.0.1 -t")[/FONT]

the Process.Start takes two arguments: the program to run and the arguements to send to that program

VB.NET:
Dim strCommandString() As String = {"ping.exe", "127.0.0.1 -t"}
Process.Start(strCommandString(0), strCommandString(1))
 
My command is running now! - How do I set the window title?

Thanks a bunch! That got my command working, but I still don't know how to specify what the command prompt window title is.

In ms-dos you can type: start "Pinging 127.0.0.1" ping 127.0.0.1 -t

It will open up a window with a title of "Pinging MyPC" and the output of the command within that Window. If you don't use the "start" command, you will see "c:\windows\system32\ping.exe" and that is it. Without the label you can't tell which host you're pinging, only the IP Address. Dealing with hundreds of IP addresses a day, this is useless without the label.

I can't seem to execute the "start" command using process start because it is not an actual executable, rather it is some sort of embedded feature within command.com.

Help!
 
That did it! Thanks a bunch!

I wanted to say thank you! This did the job and it looks great. Thanks a bunch. I cleaned up some unnecessary stuff in your Process.Start() line but other than that it was perfect.

Below is the code that I used.
VB.NET:
Process.Start("cmd", "/c title Pinging myself &&ping 127.0.0.1 -n 3")
 
cool, I found that to be working too, although the documentation stated that you had to enclose multiple commands with quotes ("ping1&&ping2&&ping3" and quotes also do double inside a quoted string)
 
Back
Top