RobertoFreemano
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
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()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
Thanks for the super speedy reply.
I tested your code; it opens cmd-prompt, but nothing else happens - see attached image.
ss.bmp
ASKER
Hi MlandaT,
I've made amendments (see code below)
but cmd doesn't open at all :(
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()
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
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.ReadToEn d()
to
Dim output As String = ps.StandardOutput.ReadToEn d()
ps.WaitForExit()
ps.WaitForExit()
Dim output As String = ps.StandardOutput.ReadToEn
to
Dim output As String = ps.StandardOutput.ReadToEn
ps.WaitForExit()
ASKER
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
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?
.Arguments = "/C ping www.google.com"
and it worked. can you hardcode like that lets see if you get a result?
ASKER
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 :(
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 :(
ASKER
If I change
.CreateNoWindow = False
Then cmd prompt appears with no data!
.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.ReadToEn d()
ps.Close()
MsgBox(output)
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.ReadToEn
ps.Close()
MsgBox(output)
ASKER
hmmmmm thanks CodeCruiser for the input.
I'm running XP (does make any difference?)
I'm running XP (does make any difference?)
ASKER
I'm testing the code on my
VB.net 2003
and
2010 Express (just incase the code is newer)
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.
ASKER
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?
ASKER
CodeCruiser: I'm not sure what has happend here, as far as I know, i've acceted MlandaT as A+ solution provider...
ASKER
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.
ASKER
Please cancel question to be closed... I wish to accept MlandaT's solution... there's been a mistake somewhere when awarding points!!!!!
ASKER
Phew,
Thanks Guys
Thanks Guys
.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.