Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
Posted

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

Fat kids are harder to kidnap
Posted

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

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
Posted

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

Posted

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

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
Posted

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()

Live as if you were to die tomorrow. Learn as if you were to live forever.
Gandhi
  • 6 months later...
Posted
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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...