Link to home
Start Free TrialLog in
Avatar of RobertoFreemano
RobertoFreemanoFlag for United Kingdom of Great Britain and Northern Ireland

asked on

copy CMD ping results back to textbox2 in VB.NET app VB.NET 2003 (preferable) or 2010 Express

Hi Experts,

I'm building an app to assist me at work... this is how it works:

1. winApp
2. Textbox1.text (type in an IP Address e.g. 10.10.10.10)
3. Textbox2.text (blank for now)
3. Button (if pressed, opens CMD and pings textbox1 data)

if possible, I would like to automatically get the ping data from CMD and copy it back to my winform TextBox2.

Lazy I know, I do this task day-in, day-out and I thought I would try building an app that condenses my work)

THE CODE BELOW OPENS CMD.EXE & PINGS TEXTBOX DATA.

Thanks,
Roberto
Dim ps As New Process
        With ps.StartInfo
            .FileName = "cmd.exe"
            .Arguments = "/K ping " & TextBox1.Text '& "-n 1"
            .CreateNoWindow = False
        End With
        ps.Start()
        ps.WaitForExit()
        ps.Close()

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Mlanda T
Mlanda T
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
PS: please also correct the line

.Arguments = "/K ping " & TextBox1.Text '& "-n 1"
.CreateNoWindow = false

to

.Arguments = "/C ping " & TextBox1.Text
.CreateNoWindow = true

...i get results using /C rather than /K
...i also normally use .CreateNoWindow = true to make it execute without opening a new window.

Avatar of RobertoFreemano

ASKER

Hi MlandaT,

Thanks for the super speedy reply.

I tested your code; it opens cmd-prompt, but nothing else happens - see attached image.
ss.bmp
Hi MlandaT,

I've made amendments (see code below)

but cmd doesn't open at all :(

Dim ps As New Process
        With ps.StartInfo
            .FileName = "cmd.exe"
            .Arguments = "/C ping " & TextBox1.Text
            .CreateNoWindow = True
            .UseShellExecute = False
            .RedirectStandardOutput = True
        End With
        ps.Start()
        ps.WaitForExit()
        Dim output As String = ps.StandardOutput.ReadToEnd()

        ps.Close()

Open in new window

interesting... these are teh results i get from running the code in snippetcompiler

i have attached both an image and the snipeetcompiler file
results.PNG
ExtractCMDOutput.vb
maybe just also try to swap around these two lines:

        ps.WaitForExit()
        Dim output As String = ps.StandardOutput.ReadToEnd()

to

        Dim output As String = ps.StandardOutput.ReadToEnd()
        ps.WaitForExit()
Do you think its not working on mine because I'm using button control?
Private Sub Button1_Clicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles Button1.Clicked
        Dim ps As New Process
        With ps.StartInfo
            .FileName = "cmd.exe"
            .Arguments = "/C ping " & TextBox1.Text
            .CreateNoWindow = True
            .UseShellExecute = False
            .RedirectStandardOutput = True
        End With
        ps.Start()
        Dim output As String = ps.StandardOutput.ReadToEnd()
        ps.WaitForExit()

        ps.Close()

    End Sub

Open in new window

no. the button control should not be a problem. what is the value of "TextBox1.Text"? I took your code and hardcoded the domain to ping....

.Arguments = "/C ping www.google.com"

and it worked. can you hardcode like that lets see if you get a result?
Hi MlandaT,

my VB skills are very limited, I seem to be able to put bits together rather than pull them out of my head :(

I've changed line code as you suggested.... .Arguments = "/C ping www.google.com"

Ran it... nothing happerns... so I checked (TaskManager) processes and 'ping.exe' appears for a couple of seconds and closes :(
If I change
.CreateNoWindow = False

Then cmd prompt appears with no data!
I can confirm that following works for me



        Dim ps As New Process
        With ps.StartInfo
            .FileName = "cmd.exe"
            .Arguments = "/C ping 10.0.0.10"
            .CreateNoWindow = True
            .UseShellExecute = False
            .RedirectStandardOutput = True
        End With
        ps.Start()
        ps.WaitForExit()
        Dim output As String = ps.StandardOutput.ReadToEnd()
        ps.Close()
        MsgBox(output)
hmmmmm thanks CodeCruiser for the input.

I'm running XP (does make any difference?)
I'm testing the code on my
VB.net 2003
and
2010 Express (just incase the code is newer)
When you open cmd manually and type ping, is it recognized? May be you Path environment variable is not set.
Ohhh - hang on,

something just happened

After a while, a box appeared
ss2.bmp
Hmm. So its taking time!
Wait. MlandaT showed you how to redirect output to a textbox and you are not giving him any credit for that?
CodeCruiser: I'm not sure what has happend here, as far as I know, i've acceted MlandaT as A+ solution provider...
Please cancel question to be closed... I wish to accept MlandaT's solution... there's been a mistake somewhere when awarding points!!!!!!
Use the object button.
Please cancel question to be closed... I wish to accept MlandaT's solution... there's been a mistake somewhere when awarding points!!!!!
Phew,

Thanks Guys