I have a number of batch files and I'm trying to wrap a GUI around them so they are easily available for some of my users all in one place.
At the moment, I've got 2 buttons and a Textbox. Each button calls the same batch file with a different working directory (though in production, the batch files will be different). At the moment, all it does is call 'DIR /A /B" to list a directory contents.
DIR /A /B
exit
I've got the following code: (Button 2 is identical, but with a different working directory)
Public Class Form1
Dim P As New Process
Dim SW As System.IO.StreamWriter
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddHandler P.OutputDataReceived, AddressOf DisplayOutput
P.StartInfo.CreateNoWindow() = True
P.StartInfo.UseShellExecute = False
P.StartInfo.WorkingDirectory = "C:\temp"
P.StartInfo.RedirectStandardInput = True
P.StartInfo.RedirectStandardOutput = True
P.StartInfo.RedirectStandardError = True
P.StartInfo.FileName = "c:\temp\dircmd.cmd"
P.Start()
P.SynchronizingObject = TextBox1
Try
P.BeginOutputReadLine()
TextBox1.Text = TextBox1.Text & vbCrLf & "Begin output" & vbCrLf
SW = P.StandardInput
SW.WriteLine()
MsgBox("WriteLine()")
SW.Dispose()
SW.Close()
MsgBox("StreamWriter Close")
P.WaitForExit()
P.CancelOutputRead()
P.Close()
MsgBox("Process Close")
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
Private Sub DisplayOutput(ByVal sendingProcess As Object, ByVal output As DataReceivedEventArgs)
Textbox1.AppendText(output.Data() & vbCrLf)
End Sub
My problem is that I'm getting the output from each command, however its getting duplicated. First button, first time works fine. Second button works, but everything is listed twice. Going back to first button, everything is listed 3 times. I initially thought I needed to clear the StreamWriter buffer, but its not repeating old info, its just repeating new info and I'm not sure why.
Begin output
C:\temp>DIR /A /B
bob.txt
dircmd.cmd
LocationRouting.xml
TempFrogsareNOTCool.txt
Test.txt
test2.txt
Test3.txt
C:\temp>exit
Begin output 2
C:\>DIR /A /B
C:\>DIR /A /B
$Recycle.Bin
$Recycle.Bin
Config.Msi
Config.Msi
Documents and Settings
Documents and Settings
<snip>
C:\>exit
C:\>exit
Switch to using a Using construct to instantiate and dispose of the Process:
Option A:
Using P As Process = New Process
P.StartInfo.CreateNoWindow = True
P.StartInfo.UseShellExecute = False
P.StartInfo.WorkingDirectory = "C:\temp"
P.StartInfo.RedirectStandardInput = True
P.StartInfo.RedirectStandardOutput = True
P.StartInfo.RedirectStandardError = True
P.StartInfo.FileName = "c:\temp\dircmd.cmd"
P.Start()
Dim sOutput As String
Using oStreamReader As System.IO.StreamReader = P.StandardOutput
sOutput = oStreamReader.ReadToEnd()
End Using
OutputTextBox.Text = sOutput
P.Close()
End Using
Option B:
Private Sub TestDirOutputButton_Click(sender As System.Object, e As System.EventArgs) Handles TestDirOutputButton.Click
Using P As Process = New Process
AddHandler P.OutputDataReceived, AddressOf DisplayOutput
P.StartInfo.CreateNoWindow() = True
P.StartInfo.UseShellExecute = False
P.StartInfo.WorkingDirectory = "C:\temp"
P.StartInfo.RedirectStandardInput = True
P.StartInfo.RedirectStandardOutput = True
P.StartInfo.RedirectStandardError = True
P.StartInfo.FileName = "c:\temp\dircmd.cmd"
P.Start()
P.SynchronizingObject = OutputTextBox
Try
P.BeginOutputReadLine()
OutputTextBox.Text = OutputTextBox.Text & vbCrLf & "Begin output" & vbCrLf
SW = P.StandardInput
SW.WriteLine()
MsgBox("WriteLine()")
SW.Dispose()
SW.Close()
MsgBox("StreamWriter Close")
P.WaitForExit()
P.CancelOutputRead()
P.Close()
MsgBox("Process Close")
Catch ex As Exception
MsgBox(ex.ToString())
End Try
RemoveHandler P.OutputDataReceived, AddressOf DisplayOutput
End Using
End Sub
Related
I have trouble creating excel or word files in folders that need to be tracked. If creating continuously using a regular new file right at the home directory trace, it will report an error like this.
I use the filesystemwatcher to keep track of the file
Here is my code
Public Sub Createee(sender As Object, e As FileSystemEventArgs)
Try
Dim x = e.Name
List1.Add(x)
Dim path As String = e.FullPath
Dim i = InStr(path, ".")
Dim y = InStr(path, "~$")
If File.Exists(path) Then
List.Add(path)
End If
If path.EndsWith(".txt") Or path.EndsWith(".docx") Or path.EndsWith(".xlsx") Or path.EndsWith(".csv") Then
If System.IO.Directory.Exists(path) Then
LbxWatching.Items.Add(Now.ToLocalTime & " - " & path & " - " & e.ChangeType.ToString)
List.Add(path)
'My.Computer.FileSystem.CopyDirectory(TbxFrom.Text, TbxTo.Text, True)
System.IO.File.Copy(path, TbxTo.Text.ToString & "\" & e.Name, True)
End If
End If
If path.EndsWith(".txt") Or path.EndsWith(".docx") Or path.EndsWith(".xlsx") Or path.EndsWith(".csv") Then
If i > 0 And y = 0 Then
If Not path.Substring(path.Length - 4) = ".tmp" And Not path.Substring(path.Length - 4) = ".TMP" Then
LbxWatching.Items.Add(Now.ToLocalTime & " - " & path & " - " & e.ChangeType.ToString)
List.Add(path)
'My.Computer.FileSystem.CopyDirectory(TbxFrom.Text, TbxTo.Text, True)
My.Computer.FileSystem.CopyFile(path, TbxTo.Text.ToString & "\" & e.Name, True)
End If
End If
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
List.Clear()
List1.Clear()
End Sub
Private Sub FileWatcher_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim appsettings = ConfigurationManager.AppSettings
Dim result As String
For Each result In appsettings
If result = "Key0" Then
TbxFrom.Text = appsettings("Key0")
ElseIf result = "Key1" Then
TbxTo.Text = appsettings("Key1")
End If
Next
MapDrive("X", TbxTo.Text)
Watcher = New FileSystemWatcher()
Watcher.Path = TbxFrom.Text
Watcher.Filter = "*.*"
Watcher.IncludeSubdirectories = True
Watcher.NotifyFilter = NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName
AddHandler Watcher.Changed, AddressOf Modify
AddHandler Watcher.Created, AddressOf Createee
AddHandler Watcher.Deleted, AddressOf Delete
AddHandler Watcher.Renamed, AddressOf ChangeName
Watcher.EnableRaisingEvents = True
CheckForIllegalCrossThreadCalls = False
LbxWatching.Items.Clear()
List = New ArrayList()
List1 = New ArrayList()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
This only happens when I create files in the monitoring folder, other things like editing, deleting files, or copying a bunch of files from elsewhere are not corrupted.
I've set up my filesystemwatcher, but I'm getting odd results. I've found that if I put a complete path of a directory, and set include subdirectories, then changes are channeled as they should. But what I've tried to do unsuccessfully is set my "C:\" drive as the path with include subdirectories.
Are there restrictions on using the main drive as a path?
I've tried every syntax of it I could, but no success. Just wondering if there's something there I don't know...comments welcome :)
Here's pertinent code if anyone wants to take a look at it. Suggestions would be great:
<PermissionSet(SecurityAction.Demand, Name:="FullTrust")>
Private Sub WatchFolders()
Dim myConn As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\kgene\source\repos\Test Program\Test Program\Resources\BakDb.accdb")
MessageBox.Show("setting up watch folders")
watcher = New FileSystemWatcher With {.NotifyFilter = NotifyFilters.DirectoryName Or NotifyFilters.FileName Or NotifyFilters.Attributes}
watcher.Path = "c:\"
watcher.IncludeSubdirectories = True
watcher.EnableRaisingEvents = True
AddHandler watcher.Changed, AddressOf LogChange
AddHandler watcher.Created, AddressOf LogChange
AddHandler watcher.Deleted, AddressOf LogChange
AddHandler watcher.Renamed, AddressOf LogChangeR
End Sub
Private Sub LogChange(ByVal source As Object, ByVal e As FileSystemEventArgs)
Dim myConn As New OleDbConnection("Provider = Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\kgene\source\repos\Test Program\Test Program\Resources\BakDb.accdb")
Dim cmd As New OleDbCommand
chkCnt = 0
Try
str = "SELECT Base, Target FROM Backup"
cmd.Connection = myConn
cmd.CommandText = str
myConn.Open()
Dim lstReader As OleDbDataReader = cmd.ExecuteReader()
If cntPlns = 0 Then
myConn.Close()
Exit Sub
End If
lstReader.Read()
Do Until chkCnt = cntPlns
If lstReader.Item(0).ToString().ToLower() = Path.GetDirectoryName(e.FullPath).ToString.ToLower() Or lstReader.Item(1).ToString().ToLower() = Path.GetDirectoryName(e.FullPath).ToString().ToLower() Then
If e.ChangeType = IO.WatcherChangeTypes.Changed Then
MessageBox.Show("first item changed")
End If
If e.ChangeType = IO.WatcherChangeTypes.Created Then
MessageBox.Show("first item create")
End If
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
MessageBox.Show("first item delete")
End If
End If
If cntPlns = 0 Then
myConn.Close()
Exit Sub
End If
lstReader.Read()
chkCnt += 1
Loop
chkCnt = 0
myConn.Close()
Catch ex As Exception
chkCnt = 0
myConn.Close()
MessageBox.Show("There was a problem conncecting to the database to watch your folders")
Exit Sub
End Try
End Sub
I moved the
watcher.EnableRaisingEvents = True
after the code that register the events as Jimi suggested. This solve the problem setting up the watched folders to register events as expected.
This is to run the openfiles.exe and return the output from it
If i run openfiles.exe at the command line it works as expected
When I run it here there is no error but i get nothing in the Messagebox
Dim NewProcess As New Process()
With NewProcess.StartInfo
.FileName = "openfiles.exe"
.Arguments = "/query /s FakeServer/fo csv /V /U FakeDomain\Fakeuser/P pword"
.RedirectStandardOutput = True
.RedirectStandardError = True
.RedirectStandardInput = True
.UseShellExecute = False
.WindowStyle = ProcessWindowStyle.Normal
.CreateNoWindow = False
End With
NewProcess.Start()
System.Threading.Thread.Sleep(5000)
MsgBox(NewProcess.StandardOutput.ReadToEnd)
This is just a sample code that I have that is similar to what you are trying to do. However, my app was trying to run a command using cmd.exe and display the result in real time to a textbox. You can try to modify it accordingly.
Dim cmd As New Process()
Dim strCommandLine As String = "Echo Hello World"
cmd.StartInfo.FileName = "cmd.exe"
cmd.StartInfo.RedirectStandardError = True
cmd.StartInfo.RedirectStandardInput = True
cmd.StartInfo.RedirectStandardOutput = True
cmd.StartInfo.CreateNoWindow = True
cmd.StartInfo.UseShellExecute = False
cmd.EnableRaisingEvents = True
Application.DoEvents()
AddHandler cmd.ErrorDataReceived, AddressOf OutputHandler
AddHandler cmd.OutputDataReceived, AddressOf OutputHandler
cmd.Start()
cmd.StandardInput.WriteLine(strCommandLine)
cmd.StandardInput.Flush()
cmd.StandardInput.Close()
cmd.BeginErrorReadLine()
cmd.BeginOutputReadLine()
cmd.Close()
Delegate Sub UpdateTextBoxDelg(text As String)
Public myDelegate As UpdateTextBoxDelg = New UpdateTextBoxDelg(AddressOf UpdateTextBox)
Public Sub UpdateTextBox(text As String)
txtOutput.Text += text & Environment.NewLine
txtOutput.SelectionStart = txtOutput.Text.Length
txtOutput.ScrollToCaret()
End Sub
Private Sub OutputHandler(sender As Object, e As DataReceivedEventArgs)
If Me.InvokeRequired = True Then
Me.Invoke(myDelegate, e.Data)
Else
UpdateTextBox(e.Data)
End If
End Sub
I run multiple command line processes, starting them in a loop. It works and starts all of them async.
Public Sub DoWork
Dim i As Integer = 0
While (Args_reader.Peek() > -1)
i = i + 1
MyArg = Args_reader.ReadLine
Dim MyArg As String
Dim MyProcess(i) As Process
MyProcess(i) = New Process
With MyProcess(i).StartInfo
.FileName = MyFile
.Arguments = MyArg
.UseShellExecute = False
.CreateNoWindow = True
.RedirectStandardInput = True
.RedirectStandardOutput = True
.RedirectStandardError = True
.WindowStyle = ProcessWindowStyle.Hidden
End With
MyProcess(i).Start()
End While
Args_reader.Close()
i = 0
End Sub
Haw can I read stdOutput for all of them and check their status?
I need to wait until they finish to continue executing the program.
I would recommend you to assign async events to every process inside a loop, so they get fired when that process has any output. Standard Output or Error Output:
Dim Proceso As New Process
'Add the event handlers:
AddHandler Proceso.OutputDataReceived, AddressOf CallbackProcesoAsync
AddHandler Proceso.ErrorDataReceived, AddressOf ErrorDataReceivedAsync
Dim startInfo As New ProcessStartInfo
startInfo.FileName = execFile
startInfo.RedirectStandardOutput = True
startInfo.RedirectStandardError = True
startInfo.CreateNoWindow = False
Proceso.StartInfo = startInfo
Proceso.Start()
the async events should be something like this:
Private Sub CallbackProcesoAsync(sender As Object, args As System.Diagnostics.DataReceivedEventArgs)
If Not args.Data Is Nothing AndAlso Not String.IsNullOrEmpty(args.Data) Then
'args.Data have the output
End If
End Sub
Private Sub ErrorDataReceivedAsync(sender As Object, args As System.Diagnostics.DataReceivedEventArgs)
If Not args.Data Is Nothing AndAlso Not String.IsNullOrEmpty(args.Data) Then
'args.Data have the output
End If
End Sub
I'm trying to redirect the output from a command-line application in VB.NET, and for some reason it fails to redirect the output. Here's my code:
Dim myProcess As Process = New Process
myProcess.StartInfo.FileName = "g++"
myProcess.StartInfo.Arguments = CMDLineCommand
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
myProcess.StartInfo.CreateNoWindow = True
myProcess.Start()
Dim output As String = myProcess.StandardOutput.ReadToEnd
myProcess.WaitForExit()
CMDLineOutputTextBox.Text = output
Does anybody know why it's not being redirected? Thanks in advance!
-Neil
EDIT: Here's my full code, in case there's anything weird with my syntax:
Dim myProcess As Process = New Process
myProcess.StartInfo.FileName = "g++"
myProcess.StartInfo.Arguments = CMDLineCommand
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
myProcess.StartInfo.CreateNoWindow = True
myProcess.EnableRaisingEvents = True
AddHandler myProcess.OutputDataReceived, AddressOf GotData
myProcess.Start()
CMDLineOutputTextBox.Text = ""
myProcess.BeginOutputReadLine()
Later on...
Private Sub GotData(sendingProcess As Object, outLine As DataReceivedEventArgs)
If Not String.IsNullOrEmpty(outLine.Data) Then
SetText(outLine.Data)
End If
End Sub
Delegate Sub SetTextCallback(value As String)
Private Sub SetText(ByVal value As String)
If Me.CMDLineOutputTextBox.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {value})
Else
Me.CMDLineOutputTextBox.Text += value + Environment.NewLine
End If
End Sub
Anything weird?
Your method will work, provided by the time you hit the line where you read the output to the end, all of the output is there. Since you are using g++, I assume that may not always be the case. You will probably be better off using the OutputDataReceived Event and capturing the data from that.
Dim myProcess As Process = New Process
myProcess.StartInfo.FileName = "ping"
myProcess.StartInfo.Arguments = "www.google.com"
myProcess.StartInfo.UseShellExecute = False
myProcess.StartInfo.RedirectStandardOutput = True
myProcess.StartInfo.RedirectStandardError = True
myProcess.StartInfo.CreateNoWindow = True
myProcess.EnableRaisingEvents = True
AddHandler myProcess.OutputDataReceived, AddressOf GotData
myProcess.Start()
myProcess.BeginOutputReadLine()
Then you handle the event like this:
Private Sub GotData(sendingProcess As Object, outLine As DataReceivedEventArgs)
If Not String.IsNullOrEmpty(outLine.Data) Then
SetText(outLine.Data)
End If
End Sub
Delegate Sub SetTextCallback(value As String)
Private Sub SetText(ByVal value As String)
If Me.TextBox3.InvokeRequired Then
Dim d As New SetTextCallback(AddressOf SetText)
Me.Invoke(d, New Object() {value})
Else
Me.TextBox3.Text += value + Environment.NewLine
End If
End Sub
I have noticed the waitforExit seems to make the code lock during the invoke.required check. When I take out the waitforexit it works.