Hi there,
I want to automate something with vb.net and display my output on message box or text box.
I came across another similar forum that matched what I wanted to do and try the method but it will not work for me:
http://bytes.com/topic/visual-basic-net/answers/721260-how-know-when-external-process-has-exited
I am not sure why rather than displaying my output, it close my form when the external program exited/closed. - Private WithEvents p As Process = New Process()
-
Private Sub RunProgram()
-
p.StartInfo.FileName = cm
-
p.StartInfo.UseShellExecute = False
-
p.StartInfo.CreateNoWindow = True
-
p.StartInfo.RedirectStandardOutput = True
-
p.StartInfo.RedirectStandardInput = True
-
p.StartInfo.RedirectStandardError = True
-
-
p.Start()
-
-
p.StandardInput.WriteLine("dir")
-
p.StandardInput.WriteLine("help")
-
End Sub()
-
-
Private Sub processExited(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles winscp.Exited
-
TextBox3.Text = "Console Output: " & vbCrLf & winscp.StandardOutput.ReadToEnd()
-
End Sub
Anyone please help.
Thanks.
5 2154
No one can advice? I really need to know how to do this....
Hi,
I don`t know if this will help you or not but, here is a way to make the process wait until it has exited and then write the StandardOutput to a textbox. - Public Class Form1
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
StartMyProcess()
-
End Sub
-
-
Sub StartMyProcess()
-
Dim proc As New Process
-
proc.StartInfo.FileName = "cmd.exe"
-
proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal
-
proc.StartInfo.CreateNoWindow = False
-
proc.StartInfo.UseShellExecute = False
-
proc.StartInfo.RedirectStandardOutput = True
-
proc.StartInfo.RedirectStandardInput = True
-
proc.Start()
-
-
proc.StandardInput.WriteLine("echo This is a test to see the feedback from the cmd window")
-
proc.StandardInput.WriteLine("exit") 'This line will make the cmd window close
-
-
proc.WaitForExit() 'This makes the process wait untill the cmd window has exited
-
-
Dim output() As String = proc.StandardOutput.ReadToEnd.Split(CChar(vbLf))
-
For x As Integer = 3 To output.Length - 1
-
If Not output(x).StartsWith("C:\") Then
-
TextBox1.Text &= output(x).Trim(CChar(vbCr)) & vbNewLine
-
End If
-
Next
-
End Sub
-
-
End Class
-
Thanks, but, I do not wanted to use the until exit method is causing the whole thing is hanging there - i still need to do something else in between. @IronRazer
Hi,
If you want to display the StandardOutput in a messagebox you can do it like this without freezing the program thread. However you may want to filter the output string before displaying it. - Public Class Form1
-
Dim proc As Process
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
StartMyProcess()
-
End Sub
-
-
Private Sub StartMyProcess()
-
proc = New Process
-
proc.StartInfo.FileName = "cmd.exe"
-
proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal
-
proc.StartInfo.CreateNoWindow = False
-
proc.StartInfo.UseShellExecute = False
-
proc.StartInfo.RedirectStandardOutput = True
-
proc.StartInfo.RedirectStandardInput = True
-
-
'Added these two lines of code to enable the process to raise events
-
proc.EnableRaisingEvents = True
-
AddHandler proc.Exited, AddressOf ProcExited
-
-
proc.Start()
-
proc.StandardInput.WriteLine("echo This is a test to see the feedback from the cmd window")
-
proc.StandardInput.WriteLine("exit") 'This line will make the cmd window close
-
End Sub
-
-
Private Sub ProcExited(ByVal sender As Object, ByVal e As System.EventArgs)
-
MessageBox.Show(proc.StandardOutput.ReadToEnd)
-
End Sub
-
End Class
-
If you want to display the StandardOutput in a TextBox which is a control it will cause a cross thread error unless you use a Delegate like this - Public Class Form1
-
Dim proc As Process
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
StartMyProcess()
-
End Sub
-
-
Private Sub StartMyProcess()
-
proc = New Process
-
proc.StartInfo.FileName = "cmd.exe"
-
proc.StartInfo.WindowStyle = ProcessWindowStyle.Normal
-
proc.StartInfo.CreateNoWindow = False
-
proc.StartInfo.UseShellExecute = False
-
proc.StartInfo.RedirectStandardOutput = True
-
proc.StartInfo.RedirectStandardInput = True
-
-
'Added these two lines of code to enable the process to raise events
-
proc.EnableRaisingEvents = True
-
AddHandler proc.Exited, AddressOf ProcExited
-
-
proc.Start()
-
proc.StandardInput.WriteLine("echo This is a test to see the feedback from the cmd window")
-
proc.StandardInput.WriteLine("exit") 'This line will make the cmd window close
-
End Sub
-
-
Private Sub ProcExited(ByVal sender As Object, ByVal e As System.EventArgs)
-
DisplayMessage(proc.StandardOutput.ReadToEnd)
-
End Sub
-
-
Private Delegate Sub DisplayMessageDel(ByVal outputstring As String)
-
-
Private Sub DisplayMessage(ByVal outputstring As String)
-
If TextBox1.InvokeRequired Then
-
Dim D As New DisplayMessageDel(AddressOf DisplayMessage)
-
TextBox1.Invoke(D, outputstring)
-
Exit Sub
-
End If
-
Dim output() As String = outputstring.Split(CChar(vbLf))
-
For x As Integer = 3 To output.Length - 1
-
If Not output(x).StartsWith("C:\") Then
-
TextBox1.Text &= output(x).Trim(CChar(vbCr)) & vbNewLine
-
End If
-
Next
-
End Sub
-
End Class
-
Hope this helps. :)
Razor, Thank you so much. You gave me exactly what I need! Thanks! :)
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Guinness Mann |
last post by:
Pardon me if this is not the optimum newsgroup for this post, but it's
the only .NET newsgroup I read and I'm certain someone here can help me.
I have a C# program that checks for an error...
|
by: orekin |
last post by:
Hi There
I have been trying to come to grips with Application.Run(),
Application.Exit() and the Message Pump and I would really appreciate
some feedback on the following questions .. There are...
|
by: Bob Day |
last post by:
Using VS 2003, VB.net...
I am confused about the Application.Exit method, where the help states "This
method does not force the application to exit." Aside from the naming
confusion, how do I...
|
by: Steven |
last post by:
Hi,
I have a program that monitors a few running applications. This
monitoring program does not itself start these running applications. My
question is how can the monitoring program detect that...
|
by: Doug Handler |
last post by:
In C# 2.0, w/ the Program.cs file, i noticed that the Application.Run(new
appname()) is run twice. I have in my constructor to do a check for a
settings file, and if it isn't found, the...
| |
by: Lonifasiko |
last post by:
Hi group,
I must launch from my Winforms application another application (.exe
developed by the client) which response time sometimes can be more or
less 30 seconds.
I'm using ProcessManager...
|
by: Chris Cairns |
last post by:
I have a MDI Application and would like to prompt the user before exit. I
placed the following in the FormClosing event. It appears to work properly,
however when a user answers no to the...
|
by: Rico |
last post by:
Hello,
I have an Access application and I would like to not only run an external
application, but at the point in the code where it is run, I'd like to wait
for the external applicaiton to...
|
by: =?Utf-8?B?TEJU?= |
last post by:
Good day,
I would like to execute an external application from a web form created
using ASP.Net. I'm using System.Diagnostics.Process. It works fine if it is
notepad.exe but it is not able to...
|
by: JasonT |
last post by:
Hi.
I have created a dll in VC++ 08 that exports a CLR windows form class. I call into this dll from an external (closed source, third-party) application to instantiate the form and embed it into...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |