none
Show result in TextBox RRS feed

  • Question

  • Hi All,

    How to show all activity in textbox1 in Sub OnChanged.

    Imports System.IO
    
    Public Class Form1
        Dim fsw As New FileSystemWatcher("C:\")
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            AddHandler fsw.Created, AddressOf OnChanged
    
            With fsw
                .EnableRaisingEvents = True
                .IncludeSubdirectories = True
                '
                ' Specif the event to watch for.
                '
                .WaitForChanged(WatcherChangeTypes.Created)
                '
                ' Specify file change notifications.
                '
                .NotifyFilter = (NotifyFilters.LastAccess Or
                                 NotifyFilters.LastWrite Or
                                 NotifyFilters.FileName Or
                                 NotifyFilters.DirectoryName)
            End With
        End Sub
        Public Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
    
            If e.ChangeType = IO.WatcherChangeTypes.Created Then
                TextBox1.Text += e.FullPath.ToString & vbCrLf
            End If
    
    
        End Sub
    
    
    End Class
    

    thank.

    Saturday, July 6, 2019 4:03 AM

Answers

  • Hi

    Managed to get this to work. Seems OK here. Form1 with multiline TextBox1 and Button1

    Imports System.IO
    Public Class Form1
    	Dim fsw As New FileSystemWatcher("C:\Users\lesha\Desktop")
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		TextBox1.Font = New Font("Courier New", 10)
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    		With fsw
    			.EnableRaisingEvents = True
    			.IncludeSubdirectories = True
    		End With
    
    		AddHandler fsw.Created, AddressOf OnChanged
    		AddHandler fsw.Deleted, AddressOf OnChanged
    		AddHandler fsw.Changed, AddressOf OnChanged
    		AddHandler fsw.Renamed, AddressOf OnRenamed
    	End Sub
    	Public Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
    		Dim timestamp As DateTime = Now
    		Select Case e.ChangeType
    			Case WatcherChangeTypes.Changed
    				Invoke(Sub() TextBox1.AppendText(timestamp.ToString("ddd, dd MMM yyyy HH:mm:ss  ").PadRight(30) & (e.ChangeType.ToString & " ").PadRight(9) & e.FullPath.ToString & vbCrLf))
    			Case WatcherChangeTypes.Created
    				Invoke(Sub() TextBox1.AppendText(timestamp.ToString("ddd, dd MMM yyyy HH:mm:ss  ").PadRight(30) & (e.ChangeType.ToString & " ").PadRight(9) & e.FullPath.ToString & vbCrLf))
    			Case WatcherChangeTypes.Deleted
    				Invoke(Sub() TextBox1.AppendText(timestamp.ToString("ddd, dd MMM yyyy HH:mm:ss  ").PadRight(30) & (e.ChangeType.ToString & " ").PadRight(9) & e.FullPath.ToString & vbCrLf))
    		End Select
    	End Sub
    	Public Sub OnRenamed(ByVal source As Object, ByVal e As RenamedEventArgs)
    		Dim timestamp As DateTime = Now
    		Invoke(Sub() TextBox1.AppendText(timestamp.ToString("ddd, dd MMM yyyy HH:mm:ss  ").PadRight(30) & (e.ChangeType.ToString & ":").PadRight(9) & e.OldFullPath.ToString & vbCrLf & "".PadRight(30) & "To:".PadRight(9) & e.FullPath & vbCrLf))
    	End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by mipakteh Saturday, July 6, 2019 11:05 AM
    Saturday, July 6, 2019 6:17 AM

All replies

  • Hi

    Managed to get this to work. Seems OK here. Form1 with multiline TextBox1 and Button1

    Imports System.IO
    Public Class Form1
    	Dim fsw As New FileSystemWatcher("C:\Users\lesha\Desktop")
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		TextBox1.Font = New Font("Courier New", 10)
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    		With fsw
    			.EnableRaisingEvents = True
    			.IncludeSubdirectories = True
    		End With
    
    		AddHandler fsw.Created, AddressOf OnChanged
    		AddHandler fsw.Deleted, AddressOf OnChanged
    		AddHandler fsw.Changed, AddressOf OnChanged
    		AddHandler fsw.Renamed, AddressOf OnRenamed
    	End Sub
    	Public Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
    		Dim timestamp As DateTime = Now
    		Select Case e.ChangeType
    			Case WatcherChangeTypes.Changed
    				Invoke(Sub() TextBox1.AppendText(timestamp.ToString("ddd, dd MMM yyyy HH:mm:ss  ").PadRight(30) & (e.ChangeType.ToString & " ").PadRight(9) & e.FullPath.ToString & vbCrLf))
    			Case WatcherChangeTypes.Created
    				Invoke(Sub() TextBox1.AppendText(timestamp.ToString("ddd, dd MMM yyyy HH:mm:ss  ").PadRight(30) & (e.ChangeType.ToString & " ").PadRight(9) & e.FullPath.ToString & vbCrLf))
    			Case WatcherChangeTypes.Deleted
    				Invoke(Sub() TextBox1.AppendText(timestamp.ToString("ddd, dd MMM yyyy HH:mm:ss  ").PadRight(30) & (e.ChangeType.ToString & " ").PadRight(9) & e.FullPath.ToString & vbCrLf))
    		End Select
    	End Sub
    	Public Sub OnRenamed(ByVal source As Object, ByVal e As RenamedEventArgs)
    		Dim timestamp As DateTime = Now
    		Invoke(Sub() TextBox1.AppendText(timestamp.ToString("ddd, dd MMM yyyy HH:mm:ss  ").PadRight(30) & (e.ChangeType.ToString & ":").PadRight(9) & e.OldFullPath.ToString & vbCrLf & "".PadRight(30) & "To:".PadRight(9) & e.FullPath & vbCrLf))
    	End Sub
    End Class
    


    Regards Les, Livingston, Scotland

    • Marked as answer by mipakteh Saturday, July 6, 2019 11:05 AM
    Saturday, July 6, 2019 6:17 AM
  • thank you very very much Leshay.

    need a little advice;

    I want stop the wacther every 30 lines and save to txt file.

    what the event to put this code.

       Dim sw As StreamWriter = File.AppendText("C:\Users\Uset\Documents\Wacther.txt")
    
            sw.WriteLine()
            For I As Integer = 0 To TextBox1.Lines.Count - 30
                sw.WriteLine(I)
            Next
            sw.Flush()
            sw.Close()


    • Edited by mipakteh Saturday, July 6, 2019 11:06 AM add
    Saturday, July 6, 2019 11:05 AM