techmanbd Posted December 4, 2003 Posted December 4, 2003 Ok I have this program that has to be run in the command window but need to use in .net so far I have this [ Dim ps As New ProcessStartInfo(Application.StartupPath & "/procmd.exe", "/1 /p16c54a") ps.RedirectStandardError = True ps.UseShellExecute = False Dim process As Process = process.Start(ps)] now when run in the command window, after the process is done it reads, and since i don't have the device connected I get this line PRO MATE not found How can I capture the message in .net so I can process the info that the device wasn't found? Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
iebidan Posted December 4, 2003 Posted December 4, 2003 try { //Your code goes here } catch (Exception ex) { //your code to manage the error goes here } this block will help you, try reading about try...catch...finally blocks, this are used to manage and filter the errors you can have in your code Cheers:D Quote Fat kids are harder to kidnap
techmanbd Posted December 4, 2003 Author Posted December 4, 2003 Sorry, I guess I didn't write that clearly. Basically the program is run like in the DOS environment, now days COMMAND PROMPT window. "PRO MATE not found" is one of many messages that after the program is run displays. I want to be able to capture what ever message comes from that program that i run through the process Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
Engine252 Posted December 5, 2003 Posted December 5, 2003 you are in the right direction you need to create the process set it redirection properties to true and then read the proces his stream it's just the last part you forgot here if you need an example lett me know Quote
techmanbd Posted December 5, 2003 Author Posted December 5, 2003 Engine Thanks, I did get it to work partly, here is what I have: Dim myProcess As Process = New Process Dim s As String myProcess.StartInfo.FileName = "cmd.exe" 'myProcess.StartInfo.Arguments = "/1 /p16f8" myProcess.StartInfo.UseShellExecute = False myProcess.StartInfo.CreateNoWindow = False myProcess.StartInfo.RedirectStandardInput = True myProcess.StartInfo.RedirectStandardError = True myProcess.StartInfo.RedirectStandardOutput = True myProcess.Start() Dim sOut As StreamReader = myProcess.StandardOutput Dim sErr As StreamReader = myProcess.StandardError Dim sIn As StreamWriter = myProcess.StandardInput sIn.AutoFlush = True sIn.Write("procmd /1 /p16f84" & System.Environment.NewLine) Dim bool As Boolean = False sIn.Write("exit" & System.Environment.NewLine) myProcess.WaitForExit() Do While sOut.Peek >= 0 s = sOut.ReadLine bool = boolPromate(s) If bool Then strPromate = s Exit Do End If Loop If Not myProcess.HasExited Then myProcess.Kill() End If sIn.Close() sOut.Close() sErr.Close() myProcess.Close() Now I get my line that I am looking for. But because the procmd program takes time to run, and I do the "Exit" to exit the whole cmd.exe process, it never catched the exit so the process never terminates. And instead of pulling up the cmd.exe i put in the procmd.exe and then ran the attribute part for "/1 /p16f54" there isn't an output. I am lmost bald pulling my hair out trying to figure away to "exit". So is there a way to monitor the procmd program while running in cmd.exe so once it finishes I can throw the "Exit" command. If I get that then the whole thing will work like I want. Thanks Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
techmanbd Posted December 5, 2003 Author Posted December 5, 2003 YES WOOHOO FINALLY GOT IT here is the code so anyone you has the same problem can see it Dim myProcess As Process = New Process Dim s As String myProcess.StartInfo.FileName = "cmd.exe" myProcess.StartInfo.UseShellExecute = False myProcess.StartInfo.CreateNoWindow = False myProcess.StartInfo.RedirectStandardInput = True myProcess.StartInfo.RedirectStandardError = True myProcess.StartInfo.RedirectStandardOutput = True myProcess.Start() Dim sErr As StreamReader = myProcess.StandardError Dim sOut As StreamReader = myProcess.StandardOutput Dim sIn As StreamWriter = myProcess.StandardInput sIn.AutoFlush = True sIn.Write("procmd /1 /p16f84" & System.Environment.NewLine) Dim bool As Boolean = False Dim intnext As Integer = 1 Do While Not myProcess.HasExited If Not myProcess.HasExited Then sIn.Write("exit" & System.Environment.NewLine) End If Loop myProcess.WaitForExit() Do While sOut.Peek >= 0 s = sOut.ReadLine bool = boolPromate(s) If bool Then strPromate = s Exit Do End If Loop sIn.Close() sOut.Close() sErr.Close() myProcess.Close() Quote Live as if you were to die tomorrow. Learn as if you were to live forever. Gandhi
qex Posted June 10, 2004 Posted June 10, 2004 the code here works that techmanbd has posted. but I have a question. Is the only way to get the output from the cmd window like this to exit first. That is the only way I have been able to get the output. Is there a way that you could leave it open wait for the output and then possibly give more input(another command) and or exit Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.