Process.startinfo, I am stuck

J

Jeff Jarrell

I have an object to execute a process. its intent is to do a simple execute
and capture stdin and stdout. Nothing fancy, not for long processes. simple.
I expect to typically use it with a fully qualified path for the executable.
If it is a *.bat file it works. If it is a *.exe it doesn't. When it
doesn't work (calling an exe file) I get an "Unrecognized Command" with an
exit code of 100. If I call a bat file it works. I have tried the
shellexecute = false, but i get a message that you can't capture stdin,
stdout. I have also tried using the workingDirectory as well to no avail.

thanks,
jeff

Public Function Execute(ByVal sExecutable As String, ByVal sParms As String,
ByVal bShowWindow As Boolean) As Integer
Reset()

Dim myProcess As Process

myProcess = New Process
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.FileName = sExecutable
myProcess.StartInfo.WorkingDirectory = ""
myProcess.StartInfo.Arguments = sParms

myProcess.StartInfo.CreateNoWindow = Not bShowWindow

myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
myProcess.Start()

Dim sOut As StreamReader = myProcess.StandardOutput
Dim sErr As StreamReader = myProcess.StandardError

Me._stdOut = sOut.ReadToEnd()
Me._stdErr = sErr.ReadToEnd

If Not myProcess.HasExited Then
myProcess.Kill()
End If

sOut.Close()
sErr.Close()

_exitCode = myProcess.ExitCode
myProcess.Dispose()

Return _exitCode

End Function
 
F

Frans Bouma [C# MVP]

Jeff said:
I have an object to execute a process. its intent is to do a simple
execute and capture stdin and stdout. Nothing fancy, not for long
processes. simple. I expect to typically use it with a fully
qualified path for the executable. If it is a *.bat file it works.
If it is a *.exe it doesn't. When it doesn't work (calling an exe
file) I get an "Unrecognized Command" with an exit code of 100. If I
call a bat file it works. I have tried the shellexecute = false, but
i get a message that you can't capture stdin, stdout. I have also
tried using the workingDirectory as well to no avail.

In your code, is sExecutable the FULL path + exe name or just the exe
name? Please specify the full path. Also, set the working directory to
the folder where your .exe is located. Did that make it work?

Btw, it can be stdout/err aren't capturable when you run an exe, for
example if it's not a console app.

FB
thanks,
jeff

Public Function Execute(ByVal sExecutable As String, ByVal sParms As
String, ByVal bShowWindow As Boolean) As Integer
Reset()

Dim myProcess As Process

myProcess = New Process
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.FileName = sExecutable
myProcess.StartInfo.WorkingDirectory = ""
myProcess.StartInfo.Arguments = sParms

myProcess.StartInfo.CreateNoWindow = Not bShowWindow

myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
myProcess.Start()

Dim sOut As StreamReader = myProcess.StandardOutput
Dim sErr As StreamReader = myProcess.StandardError

Me._stdOut = sOut.ReadToEnd()
Me._stdErr = sErr.ReadToEnd

If Not myProcess.HasExited Then
myProcess.Kill()
End If

sOut.Close()
sErr.Close()

_exitCode = myProcess.ExitCode
myProcess.Dispose()

Return _exitCode

End Function



--
 
G

Guest

If it is a *.bat file it works. If it is a *.exe it doesn't.

My formula for an exe is shown below. I stopped experimenting when I got it
to work, so I don't know precisely what is necessary and what is not.

Dim s as String
Dim p As New Process
With p.StartInfo
.FileName = "..." ' exe path goes here
.Arguments = "..." ' args go here
.UseShellExecute = False
.RedirectStandardError = True
.RedirectStandardInput = True
.RedirectStandardOutput = True
.WindowStyle = ProcessWindowStyle.Hidden
.CreateNoWindow = True
End With
Try
p.Start()
s = p.StandardOutput.ReadToEnd()
p.WaitForExit()
Catch e As Exception
s = e.tostring
End Try
 
J

Jeff Jarrell

I put your stuff in and it worked. I hope to get a chance to dig into why
it wasn't work for me before...

thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top