Respond to commandline program question in VB.net - vb.net

I'm using VB.net to have a GUI on top of a commandline program.
Dim sqliProcess As New Process()
sqliProcess.StartInfo.UseShellExecute = False
sqliProcess.StartInfo.RedirectStandardOutput = True
sqliProcess.StartInfo.RedirectStandardError = True
sqliProcess.StartInfo.FileName = "C:\shell_program.exe"
sqliProcess.StartInfo.CreateNoWindow = True
sqliProcess.Start()
Do While Not bw.CancellationPending
Try
If Not sqliProcess.StandardOutput.EndOfStream Then
Debug.Print(sqliProcess.StandardOutput.ReadLine)
End If
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Error reading output")
End Try
Threading.Thread.Sleep(1)
Loop
Which works and prints out all output from the commandline program. However at some point the commandline program ask for user interaction, eg:
Type a number (1/2/3/4):
But the commandline program stops after this. I suspect this is because it doesn't receive a valid option.
Is there a way to capture when the commandline program wants user interaction and hold the reading of the stream to be able to enable the user to input something?

It's not that hard if you use the events. (Don't forget EnableRaisingEvents). Look here at outputdatareceived..

Related

Process: StandardOutput

I try to communicate between 2 processes, one written in Vb.net and the other written in Tcl. My goal is to start the program in tcl through System.Diagnostics.Process() and communicate with the standard output of Tcl.
Dim form As New System.Diagnostics.Process()
form.StartInfo.RedirectStandardOutput = True
form.StartInfo.RedirectStandardError = False
form.StartInfo.UseShellExecute = False
form.StartInfo.CreateNoWindow = True
form.StartInfo.Arguments = myargs
form.StartInfo.FileName = "wish.exe"
form.Start()
Dim output As String = Nothing
While Not form.StandardOutput.EndOfStream
output = form.StandardOutput.ReadLine()
If output.Contains("IMAGE=") Then
' Do stuff...
End If
End While
form.Close()
It works, but I would like to send an info to my Vb.net program to tell it to stop like this :
If output.Contains("EXIT") Then
Exit While
End If
And finally let my Tcl program redirect its standard output to my wish.exe console like this :
# communication with vb.net program
foreach prt $fileimage {
puts "IMAGE=$prt"
}
# exit vb.net program
puts "EXIT"
# puts others 'info' in my wish.exe console
puts "info..." ; # problem here > 'info...' is not visible in my console
The problem I have is that when I exit my program, the standard output of Tcl is no longer redirected to my console...
My question : What is the best way to get out of my program without losing the standard Tcl output?

If explorer is already running

I have a system that backs up data to a network with certain parameters in place such as:
'If x process is running, do not run the backup'
Because it's connected to a network, users will constantly be on it. The problem I have is that I don't want the data to get moved onto the network if it's in use, plus, the data is unable to move across if someone is using file explorer on the same computer as the program is on.
I would use 'If explorer is running, do not run the backup' but explorer is linked to windows and is always running
If program.Count > 0 Or program2.Count > 0 Then
Try
Msgbox("Process Running")
Catch ex As Exception
End Try
Else
'backup data
End If
Is there a way to try get the program to transfer files, but if fails because the file directory is already open, then do x?
Try this. It should generate an error if you can't lock the file.
Public Function IsFileLocked(file As FileInfo) As Boolean
Dim stream = DirectCast(Nothing, FileStream)
Try
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None)
Catch generatedExceptionName As IOException
'handle the exception your way
Return True
Finally
If stream IsNot Nothing Then
stream.Close()
End If
End Try
Return False
End Function

Run Internet Explorer as a different user

I'm trying to launch Internet Explorer with a different user to access a website that requires single sign on.
Below is the code that I'm using. It doesn't produce any errors but IE doesn't launch at all, not even the process.
Try
System.Diagnostics.Process.Start(IExplorerPath, Username, ConvertToSecureString(Password), Domain)
Success = True
Catch ex As Exception
Success = False
Error_Message = ex.Message
End Try
I also tried the following variation with the same result (nothing):
Try
Dim psi As New ProcessStartInfo()
psi.Filename = IExplorerPath
psi.UserName = Username
psi.Domain = Domain
psi.Password = ConvertToSecureString(Password)
psi.UseShellExecute = False
Process.Start(psi)
Success = True
Catch ex As Exception
Success = False
Error_Message = ex.Message
End Try
This is the ConvertToSecureString function:
Function ConvertToSecureString(ByVal str As String)
Dim password As New SecureString
For Each c As Char In str.ToCharArray
password.AppendChar(c)
Next
Return password
End Function
Ok, I've been searching a lot a solution for this and if you have it you deserve a Nobel price. In the mean time, if it helps anyone this is the workaround that I had to use:
Use Process.Start to run a .bat file that contains the following code:
runas /user:User#Domain.com "C:\Program Files\Internet Explorer\iexplore.exe"
Then, when the command prompt appears I use the Send Keys to type in the password and press Enter.
This launches IE with different credentials. But again, there should be a better way.
This may / may not work. Try to launch your website that requires single sign on in "CHROME", hit F-12, go to Application Tab -> Cookies -> Click on your site link. on left hand side look for something that represent your session id, may be JSESSIONID or similar, copy that. Now open your Internet Explorer, hit F-12 and manually create that JSESSIONID by running this command in console window
document.cookie = "JSESSIONID=<your-session-id-from-chrome"
hit play button to execute script
Refresh your browser

How do I load the same Crystal Report twice?

For some reason I can't load this report twice in one program session. I can print it the first time without a problem. The second time I get the error:
CrystalDecisions.Shared.CrystalReportsException: Load report failed.
Here is my code for this segment.
Private Sub Print_Report()
Using CrystalReport As New ReportDocument
CrystalReport.Load("Reports\PrintMe.rpt")
CrystalReport.SetParameterValue("Code", txtCode.Text)
CrystalReport.SetParameterValue("Control", txtControl.Text)
CrystalReport.PrintOptions.PrinterName = DefaultPrinterName()
CrystalReport.PrintToPrinter(1, True, 0, 0)
End Using
End Sub
Private Function DefaultPrinterName() As String
Dim psDefault As New System.Drawing.Printing.PrinterSettings
Try
DefaultPrinterName = psDefault.PrinterName
Catch ex As System.Exception
DefaultPrinterName = ""
Finally
psDefault = Nothing
End Try
End Function
All I can think is that the file isn't being let go at the end of the first print. Am I over looking something simple?
The issues was caused by using Foxit Reader's PDF Printer. I switched to using Adobe Acrobat and the issue is now gone. I'm only using the PDF Printer for printer tests to save paper. I will be using the Export to Disk for anything PDF related.

Get output of external console app to RichTextBox

I used the VB.Net Shell() command to start a console app that I haven't created. I want to get the lines from that console to a RichTextBox in my form. It should be possible because I have seen many apps that do this. Please provide some information or any program that might help me. I tried to see if the external app creates log files, but it does not.
Here's how I started. What should I add, and where, to return the output?
Try
writer.WriteLine(RichTextBox1.Text)
writer.Close()
Shell(CurDir() & "\yelloeye.exe .\new.demo")
Catch ex As Exception
MsgBox(ex.Message)
End Try
Here's an example using the RedirectStandardOutput property as mentioned by #Plutonix which will ping SO and display the results in a RichTextBox. Just replace the .exe name and arguments (if any) to suit your needs.
Private Sub ShellToRTB()
Dim p As New Process()
p.StartInfo.FileName = "ping.exe"
p.StartInfo.Arguments = "www.stackoverflow.com"
p.StartInfo.UseShellExecute = False
p.StartInfo.RedirectStandardOutput = True
p.Start()
RichTextBox1.Text = p.StandardOutput.ReadToEnd()
p.WaitForExit()
End Sub
Result: