about using Shell

knowmeifyou

Active member
Joined
Jun 16, 2013
Messages
26
Programming Experience
Beginner
this is my codes

VB.NET:
Try            
            Shell("cmd.exe")
            SendKeys.Send("ping -t")
            'SendKeys.Send("{ENTER}")
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

but the output is wrong, it runs two CMD
the one doesn't respond with the sendkeys
and the other one print on the cmd is wrong

just like this

Untitled.png
 
Well, the first thing to know about using Shell is not to use it. It's a holdover from VB6 and there's no good reason to use it unless you have an upgraded VB6 app that already uses Shell. If you're writing new VB.NET code then you should be using Process.Start.

A point of particular note with Process.Start is that it provides the ability to redirect the input and output streams of the new process, so you can send an instruction directly to the app and not use SendKeys, which is extremely dodgy.

There's no need to do that anyway though. "ping" is actually an application itself, i.e. C:\Windows\System32\PING.EXE, so you can run it directly using Process.Start.

There's no need to even use Process.Start if your intention is to use ping though. The .NET Framework includes a Ping class that you should use instead.
 
Back
Top