Commandprompt and vb.net

BrechtLS

Active member
Joined
Apr 5, 2006
Messages
32
Location
Belgium
Programming Experience
1-3
[FONT=verdana, arial, helvetica]Hi,
[/FONT]
[FONT=verdana, arial, helvetica]I should want to add a command (for example "ipconfig") to the commandprompt and use the output of the command in my program (show it in a textbox, ...). It is the meaning that the user of the program doesn't has to do anything in the commandprompt. And that the commandprompt closes automatically.[/FONT][FONT=verdana, arial, helvetica]

Any ideas?
[/FONT]
Thanks.
 
I got a couple of problems with that code:
If I use the code in the function, all the lines stand after eachother.
When I use the code in the sub it first works great but after a couple of times I don't get anything anymore (its totaly blank), I need to input the code again to make it work again.
Any ideas how I can solve this?
 
I'll repeat my last question.

When I use this code:
VB.NET:
Shared Function GetProcessText(ByVal process As String, ByVal param As String, ByVal workingDir As String) As String 
Dim p As Process = New Process 
[COLOR=green]' this is the name of the process we want to execute[/COLOR] 
p.StartInfo.FileName = process 
If Not (workingDir = "") Then 
p.StartInfo.WorkingDirectory = workingDir 
End If 
.StartInfo.Arguments = param
[COLOR=green]' need to set this to false to redirect output[/COLOR]
p.StartInfo.UseShellExecute = False 
p.StartInfo.RedirectStandardOutput = True 
[COLOR=green]' start the process [/COLOR]
p.Start 
[COLOR=green]' read all the output
' here we could just read line by line and display it
' in an output window [/COLOR]
Dim output As String = p.StandardOutput.ReadToEnd
[COLOR=green]' wait for the process to terminate [/COLOR]
p.WaitForExit 
Return output 
End Function
All the lines in the output of the command, stand after each other, like this:
Windows IP configuratie IP-adres:..............192.168.2.5
But they schould stand under each other.

When I use this code:
VB.NET:
Private Sub GetProcessText(ByVal process As String, ByVal param As String, ByVal workingDir As String)
  Dim p As Process = New Process 'This is the name of the process we want to execute 
  p.StartInfo.FileName = process
  If Not (workingDir = "") Then
    p.StartInfo.WorkingDirectory = workingDir
  End If
  p.StartInfo.Arguments = param
  p.StartInfo.UseShellExecute = False 'need to set this to false to redirect output
  p.StartInfo.RedirectStandardOutput = True
  p.StartInfo.CreateNoWindow = True
  p.Start()
  ' read all the output
  Dim SROutput As System.IO.StreamReader = p.StandardOutput
  Dim tmp As String
  Do While p.HasExited = False
    tmp = SROutput.ReadLine
    If tmp <> "" Then
      Me.TextBox1.AppendText(tmp & vbNewLine)
    End If
  Loop
  p.Dispose()
End Sub
It works several times, but then the output is blanc. I need to input the code again to get the output of the command again.

How can I solve this?
It should be great, if one of the two works.
Thanks.
 
BrechtLS said:
It works several times, but then the output is blanc. I need to input the code again to get the output of the command again.
Sorry, I can't reproduce your problem. Also I don't know what you mean with "input the code again".
 
I mean I have to delete next code and type it in again:
VB.NET:
[LEFT]Private Sub GetProcessText(ByVal process As String, ByVal param As String, ByVal workingDir As String)
  Dim p As Process = New Process 'This is the name of the process we want to execute 
  p.StartInfo.FileName = process
  If Not (workingDir = "") Then
    p.StartInfo.WorkingDirectory = workingDir
  End If
  p.StartInfo.Arguments = param
  p.StartInfo.UseShellExecute = False 'need to set this to false to redirect output
  p.StartInfo.RedirectStandardOutput = True
  p.StartInfo.CreateNoWindow = True
  p.Start()
  ' read all the output
  Dim SROutput As System.IO.StreamReader = p.StandardOutput
  Dim tmp As String
  Do While p.HasExited = False
    tmp = SROutput.ReadLine
    If tmp <> "" Then
      Me.TextBox1.AppendText(tmp & vbNewLine)
    End If
  Loop
  p.Dispose()
End Sub[/LEFT]
 
I really can't see how deleting some code and then type it back in would change anything.
 
It did suprise me too but it seems to be the only way to make it work again.
 
Back
Top