VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Windows Service with File System Watcher Event. When File Found This Application Create Log Automat

by Geha Fernando (1 Submission)
Category: Files/File Controls/Input/Output
Compatability: VB.NET
Difficulty: Unknown Difficulty
Originally Published: Thu 30th October 2008
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Windows Service with File System Watcher Event. When File Found This Application Create Log Automatically To The System Log.

API Declarations


.IO
.Diagnostics

Rate Windows Service with File System Watcher Event. When File Found This Application Create Log Automat




    'How To Install Windows Service
    '------  C:\<WinPath>\Microsoft.NET\Framework\v2.0.50727>InstallUtil ServicePath

    'Remove Windows Service
    '------  C:\<WinPath>\Microsoft.NET\Framework\v2.0.50727>InstallUtil /u ServicePath

    Private WithEvents Fsystem As New FileSystemWatcher()
    Private Path As String = "E:\Temp Data"
    Private Iscomplete As Boolean = True
    Private prc As Process

    Protected Overrides Sub OnStart(ByVal args() As String)

        Try
            With Fsystem
                .Path = Path
                .Filter = "*.txt"
                .NotifyFilter = NotifyFilters.CreationTime Or NotifyFilters.DirectoryName Or NotifyFilters.FileName Or _
                               NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.Security
            End With

            AddHandler Fsystem.Created, AddressOf Fsystem_Created
            AddHandler Fsystem.Deleted, AddressOf Fsystem_Deleted

            Fsystem.EnableRaisingEvents = True
            Call WriteToEventLog("Folder MonitorS ervice starts sucessfully.", "FolderMonitor")
        Catch ex As Exception
            Call WriteToEventLog(ex.Message, "FolderMonitor", EventLogEntryType.Error)
        End Try

    End Sub
    Protected Overrides Sub OnStop()

        Fsystem.EnableRaisingEvents = False
        Call WriteToEventLog("Folder MonitorS ervice stops sucessfully.", "FolderMonitor")

    End Sub
    Private Sub Fsystem_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles Fsystem.Created

        Try
            If Iscomplete = True Then
                Iscomplete = False
                Call WriteToEventLog("Notepad opened sucessfully.", "FolderMonitor")

                prc = New Process()
                With prc.StartInfo
                    .FileName = "notepad.exe"
                    .WindowStyle = ProcessWindowStyle.Normal
                    .CreateNoWindow = False
                End With

                prc.Start()

                prc.Close() : prc.Dispose()
            End If
        Catch ex As Exception
            Call WriteToEventLog(ex.Message, "FolderMonitor", EventLogEntryType.Error)
        End Try

    End Sub
    Private Sub Fsystem_Deleted(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles Fsystem.Deleted

        If Iscomplete = False Then
            Iscomplete = True
            Call WriteToEventLog("File deleted sucessfully.", "FolderMonitor")
        End If

    End Sub
    Public Sub WriteToEventLog(ByVal Entry As String, _
                               Optional ByVal AppName As String = "VB.NET Application", _
                               Optional ByVal EventType As  _
                               EventLogEntryType = EventLogEntryType.Information, _
                               Optional ByVal LogName As String = "Application")

        Dim objEventLog As New EventLog()
        If Not EventLog.SourceExists(AppName) Then EventLog.CreateEventSource(AppName, LogName)
        objEventLog.Source = AppName
        objEventLog.WriteEntry(Entry, EventType)

    End Sub

End Class

Download this snippet    Add to My Saved Code

Windows Service with File System Watcher Event. When File Found This Application Create Log Automat Comments

No comments have been posted about Windows Service with File System Watcher Event. When File Found This Application Create Log Automat. Why not be the first to post a comment about Windows Service with File System Watcher Event. When File Found This Application Create Log Automat.

Post your comment

Subject:
Message:
0/1000 characters