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

RobertoFreemanoAsked:
Who is Participating?
 
MlandaTCommented:
try these changes to you code:

Dim ps As New Process
        With ps.StartInfo
            .FileName = "cmd.exe"
            .Arguments = "/K ping " & TextBox1.Text '& "-n 1"
            .CreateNoWindow = False

           .UseShellExecute = false
            .RedirectStandardOutput = true


        End With
        ps.Start()
        ps.WaitForExit()

       Dim output as string = p.StandardOutput.ReadToEnd()

        ps.Close()


0
 
MlandaTCommented:
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.

0
 
RobertoFreemanoAuthor Commented:
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
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
RobertoFreemanoAuthor Commented:
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

0
 
MlandaTCommented:
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
0
 
MlandaTCommented:
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()
0
 
RobertoFreemanoAuthor Commented:
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

0
 
MlandaTCommented:
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?
0
 
RobertoFreemanoAuthor Commented:
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 :(
0
 
RobertoFreemanoAuthor Commented:
If I change
.CreateNoWindow = False

Then cmd prompt appears with no data!
0
 
CodeCruiserCommented:
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)
0
 
RobertoFreemanoAuthor Commented:
hmmmmm thanks CodeCruiser for the input.

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

something just happened

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

Thanks Guys
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.