How do I output from a PowerShell script (PSObjects?) to a WinForms TextBox in real-time? - vb.net

I am executing a PowerShell script from a Visual Basic WinForms UI, and I managed to code it so it executes on a BackgroundWorker thread so that the UI doesn't lock up while the script is running. What it does is imports a .printerExport file to add printers and drivers to a target host, and it runs great. The only issue is that I set the output of the PSObjects to a TextBox and they all get output at the end once the script is completed, rather than output in real time like it would in a PowerShell console window.
I have tried multiple things to get it to output in real-time, but I am out of ideas, and even ReportProgress doesn't manage to get the real-time output as well.
How can I get this done? Below are the three involved Sub functions I am currently using:
Private Sub BackgroundWorker1_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim HostName As String
Dim PrintName As String
Dim SourceFilePath As String
Dim DestinationPath As String
Dim FileName As String
HostName = txtHostName.Text
PrintName = "\\" & txtHostName.Text
SourceFilePath = txtFilePath.Text
DestinationPath = PrintName & "\c$\Temp\"
If String.IsNullOrEmpty(HostName) Or String.IsNullOrEmpty(SourceFilePath) Then
MessageBox.Show("Please enter the target host name and choose a file path.")
Return
End If
FileName = Path.GetFileName(SourceFilePath)
File.Copy(SourceFilePath, Path.Combine(DestinationPath, Path.GetFileName(SourceFilePath)), True)
Dim PsEnv As New RunspaceInvoke
Dim App As String = $"Invoke-Command -ComputerName {HostName} {{C:\Windows\System32\spool\tools\Printbrm.exe -r -s {PrintName} -f ""C:\Temp\{FileName}""}}"
Dim AppObjects As Collection(Of PSObject) = PsEnv.Invoke(App)
Dim Output As New StringBuilder()
Dim id As Integer = 0
For Each psobj2 As PSObject In AppObjects
id += 1
BackgroundWorker1.ReportProgress(id, psobj2.ToString() & vbCrLf & vbCrLf)
Next
End Sub
Private Sub BackgroundWorker1_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
Dim userState As String = CType(e.UserState, String)
TextBox3.AppendText(userState)
End Sub
Private Sub buttonInstall_Click(sender As Object, e As EventArgs) Handles buttonInstall.Click
BackgroundWorker1.WorkerReportsProgress = True
BackgroundWorker1.RunWorkerAsync()
End Sub

Related

Copy a File Using DragDrop VB.Net

What is wrong with my code? Getting a 'Process cannot access the file because it is being used by another process' error msg Is there a way around this. My google-fu was not giving me much luck. I was not able to Move or Copy, and I will take either.
Private Sub frmFiberTransMain_DragEnter(sender As Object, e As DragEventArgs) Handles MyBase.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop, False) = True Then
e.Effect = DragDropEffects.All
End If
End Sub
Private Sub frmFiberTransMain_DragDrop(sender As Object, e As DragEventArgs) Handles MyBase.DragDrop
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
Dim filePaths As String() = CType(e.Data.GetData(DataFormats.FileDrop), String())
Call CopyFileDrop(filePaths)
End If
End Sub
Private Sub CopyFileDrop(filePaths As String())
For Each fileLoc As String In filePaths
Dim fileName As String = fileLoc
Dim fi As New IO.FileInfo(fileName)
File.Create(fileName)
Dim justFileName As String = fi.Name
Dim newPathName As String = gProgDir & "\" & justFileName
Directory.Move(fileLoc, newPathName)
Next fileLoc
End Sub
File.Create(fileName) returns an open handle and you're not closing it. It doesn't look like you need that line. –
#LarsTech 50 mins ago

VB.Net FileSystemWatcher - Multiple Folders and Multiple Files

I have a requirement of monitoring a root folder under which there are multiple folders and each folder containing multiple files (.csv) These files are being copied to a workstation from a server using some other third party tool. I have to monitor the workstation root's folder and as an when a .csv file is created or updated in any of the child folders, I have to write the .csv file data (updated or new) to a sql server database. While writing the data to the sql server, I need to capture the child folder name in which the .csv file was updated/created. This way I can have separate folder's data in one database file.
While running the program on the workstation pc, the application crashes without any specific error message. And since the app is run on a remote pc, I cannot debug the app to check which line is causing the crash. Below is the code:
Public Class FolderWatchAndProcess
Dim blnMonitorUpdated As Boolean = True
Dim blnMonitorCreated As Boolean = True
Dim blnShowMessage As Boolean
Dim strFullFileName As String
Dim strFileName As String
Dim strFolderName As String
Private Delegate Sub updateLabel(ByVal newLabel As String)
Private Sub updateLabelHandler(ByVal labelText As String)
lblMetNo.Text = labelText
End Sub
Private Sub ReadAndInsertCSVData()
' This routine checks for duplicates and inserts new records into the sql server database
End Sub
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'lblMonitorPath.Text = "C:\RootFolder\"
'btnStartWatching_Click(Nothing, Nothing)
'Me.WindowState = FormWindowState.Minimized
End Sub
Private Sub btnStartWatching_Click(sender As Object, e As EventArgs) Handles btnStartWatching.Click
btnStartWatching.Text = "Stop Watching"
Dim aFileWatcherInstance As ArrayList = New ArrayList
For Each sMonitorFolder As String In lstFolders.Items
If IO.Directory.Exists(sMonitorFolder) Then
Dim oFileWatcher As FileSystemWatcher = New FileSystemWatcher
oFileWatcher.Path = sMonitorFolder
oFileWatcher.Filter = "*.CSV"
If blnMonitorUpdated Then
AddHandler oFileWatcher.Changed, AddressOf Me.FileSystemWatcherUpdated
Else
'RemoveHandler , AddressOf Me.FileSystemWatcherUpdated
End If
If blnMonitorCreated Then
AddHandler oFileWatcher.Created, AddressOf Me.FileSystemWatcherCreated
Else
'RemoveHandler , AddressOf Me.FileSystemWatcherCreated
End If
oFileWatcher.EnableRaisingEvents = True
aFileWatcherInstance.Add(oFileWatcher)
End If
Next
End Sub
Private Sub FileSystemWatcherCreated(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
strFullFileName = e.FullPath
strFileName = Mid(e.Name, 3, Len(e.Name))
strFolderName = strFileName.Split("_")(0)
If Me.InvokeRequired Then
Dim d As New updateLabel(AddressOf updateLabelHandler)
Me.BeginInvoke(d, New Object() {strFolderName})
Else
updateLabelHandler(strFolderName)
End If
ReadAndInsertCSVData()
End Sub
Private Sub FileSystemWatcherDeleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
'A file has been deleted from the child directory.
End Sub
Private Sub FileSystemWatcherUpdated(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
strFullFileName = e.FullPath
strFileName = Mid(e.Name, 3, Len(e.Name))
strFolderName = strFileName.Split("_")(0)
If Me.InvokeRequired Then
Dim d As New updateLabel(AddressOf updateLabelHandler)
Me.BeginInvoke(d, New Object() {strFolderName})
Else
updateLabelHandler(strFolderName)
End If
ReadAndInsertCSVData()
End Sub
End Class
Any help would be appreciated.
the Changed event will fire multiple times:
https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.changed(v=vs.110).aspx
probably you can use the IsFileLocked function that mention at here:
I keep getting exception sometimes on System.Threading.Tasks.TaskCompletionSource<bool> how can i solve it?

Variables are used before it is assigned a null value. Using StreamReader

I decided to try one of the advanced exercises in my book, it needs me to edit a program. Before getting the artist name and price, the btn.Add_Click procedure should determine whether the CD name is already included in the list box. If the list box contains the CD name, the procedure should display an appropriate message and then not add the CD to the list. Problem is the Do Until inFile.Peek = -1 and the For Each Name As String In strNameCollection apparently are assigned a null value. Any ideas on why this is or possibly how to get this to work? btn.Add_Click is towards the bottom of the code
Option Explicit On
Option Strict On
Option Infer Off
Public Class frmMain
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
Private Sub frmMain_FormClosing(sender As Object, e As Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
' save the list box information
' declare a StreamWriter variable
Dim outFile As IO.StreamWriter
' open the file for output
outFile = IO.File.CreateText("CDs.txt")
' write each line in the list box
For intIndex As Integer = 0 To lstCds.Items.Count - 1
outFile.WriteLine(lstCds.Items(intIndex))
Next intIndex
' close the file
outFile.Close()
End Sub
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
' fills the list box with data
' stored in a sequential access file
' declare variables
Dim inFile As IO.StreamReader
Dim strInfo As String
' verify that the file exists
If IO.File.Exists("CDs.txt") Then
' open the file for input
inFile = IO.File.OpenText("CDs.txt")
' process loop instructions until end of file
Do Until inFile.Peek = -1
strInfo = inFile.ReadLine
lstCds.Items.Add(strInfo)
Loop
inFile.Close()
' select the first line in the list box
lstCds.SelectedIndex = 0
Else
MessageBox.Show("Can't find the CDs.txt file",
"CD Collection",
MessageBoxButtons.OK,
MessageBoxIcon.Information)
End If
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
' adds CD information to the list box
' declare variables
Dim strName As String
Dim strArtist As String
Dim strPrice As String
Dim strConcatenatedInfo As String
Dim dblPrice As Double
Dim strNameCollection() As String
'read all names into array
Dim inFile As IO.StreamReader
If IO.File.Exists("CDs.txt") Then
' open the file for input
inFile = IO.File.OpenText("CDs.txt")
End If
'read all names into array
Dim index As Integer = 0
Do Until inFile.Peek = -1
ReDim strNameCollection(index)
strNameCollection(index) = inFile.ReadLine
Loop
inFile.Close()
' get the CD information
strName = InputBox("CD name:", "CD Collection")
For Each Name As String In strNameCollection
If strName = Name Then
MessageBox.Show("Sorry that name was alread on the list", "CD Project",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
Else
strArtist = InputBox("Artist:", "CD Collection")
strPrice = InputBox("Price:", "CD Collection")
Double.TryParse(strPrice, dblPrice)
strPrice = dblPrice.ToString("N2")
strConcatenatedInfo = strName.PadRight(40) &
strArtist.PadRight(25) & strPrice.PadLeft(5)
lstCds.Items.Add(strConcatenatedInfo)
End If
Next Name
End Sub
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
' removes the selected line from the list box
' if a line is selected, remove the line
If lstCds.SelectedIndex <> -1 Then
lstCds.Items.RemoveAt(lstCds.SelectedIndex)
End If
End Sub
End Class
Why not take advantage of the Using blocks? Make strNameCollection a List(Of String) instead of an array that you are not increasing it's size correctly.
Private strNameCollection As New List(Of String)
Using sr As New StreamReader("CDs.txt")
While Not sr.EndOfStream
strNameCollection.Add(sr.ReadLine)
End while
End Using ' file closed and stream disposed

GetDirectory will not list all Directories

I have a form that allows you to click a button, which triggers an OpenFileDialog. From there, you are suppose to select a specific file within that folder, and then the program is supposed to go through from the folder you were in the the /subjects folder and list those directories.
At the moment, I have 3 directories within /subjects: english, mathematics, and cte.
My issue is that when the program is ran, it will only list the English directory in the combo-box, and will not list any of the others.
Private Sub btnDocumentChoice_Click(sender As Object, e As EventArgs) Handles btnDocumentChoice.Click
Dim ofd As New OpenFileDialog
Dim DirList As New ArrayList
If ofd.ShowDialog = Windows.Forms.DialogResult.OK AndAlso ofd.FileName <> "" Then
strRootLocation = (Path.GetDirectoryName(ofd.FileName))
GetDirectories(strRootLocation + "/subject/", DirList)
'MessageBox.Show(Path.GetDirectoryName(ofd.FileName))
End If
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
strRootLocation = OpenFileDialog1.FileName
cmbSubject.Items.Add(strRootLocation)
End Sub
Sub GetDirectories(ByVal StartPath As String, ByRef DirectoryList As ArrayList)
Dim Dirs() As String = Directory.GetDirectories(StartPath)
DirectoryList.AddRange(Dirs)
For Each Dir As String In Dirs
GetDirectories(Dir, DirectoryList)
cmbSubject.Items.Add(Replace(Path.GetDirectoryName(Dir), strRootLocation + "\subject", ""))
cmbSubject.Items.Remove("")
Next
End Sub
I managed to fix my own issue by removing the For Each loop in the question, and replacing it with this:
Dim directories As String
For Each directories In Directory.GetDirectories(strRootLocation + "\subject")
cmbSubject.Items.Add(Replace(directories, strRootLocation + "\subject\", ""))
Next

Wait X seconds between ReadLine of text file

What I want is for the app to read the first line from a text file and output it to the textReply textbox, then wait for X time before reading and outputting the next line.
Dim fileIn As New System.IO.StreamReader("C:\test.txt")
Dim strData As String = ""
While (Not (fileIn.EndOfStream))
strData = fileIn.ReadLine()
textReply.Text = textReply.Text & strData & vbCr
System.Threading.Thread.Sleep(1000)
End While
As you can see I have tried sleeping the thread (not ideal as I want the app to stay responsive but thats another matter!) but every time it reads the whole file and dumps the lot into the textbox.
The reason is eventually it will be used for a serial connection to a device which needs time for the data transfer to the device and for the device to respond to each line sent.
You can create a Queue of string add each line to the list as you are reading the file, then you can poll it with a timer and add them to your textbox one at a time.
Public Class Form1
Dim myQueue As Queue(Of String) = New Queue(Of String)
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim fileIn As New System.IO.StreamReader("C:\test.txt")
Timer1.Start()
Dim strData As String = ""
While (Not (fileIn.EndOfStream))
strData = fileIn.ReadLine()
myQueue.Enqueue(strData)
End While
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If myQueue.Count > 0 Then
textReply.Text += myQueue.Dequeue & vbCrLf
End If
End Sub
End Class