Filewatcher amd making a service


Imports System.ServiceProcess 
Imports System.Threading 

Public Class TriMechPDM 
    Inherits System.ServiceProcess.ServiceBase 

#Region " Component Designer generated code " 

    Public Sub New() 
        MyBase.New() 

        ' This call is required by the Component Designer. 
        InitializeComponent() 

        ' Add any initialization after the InitializeComponent() call 

    End Sub 

    'UserService overrides dispose to clean up the component list. 
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) 
        If disposing Then 
            If Not (components Is Nothing) Then 
                components.Dispose() 
            End If 
        End If 
        MyBase.Dispose(disposing) 
    End Sub 

    ' The main entry point for the process 
    <MTAThread()> _ 
    Shared Sub Main() 
        Dim ServicesToRun() As System.ServiceProcess.ServiceBase 

        ' More than one NT Service may run within the same process. To add 
        ' another service to this process, change the following line to 
        ' create a second service object. For example, 
        ' 
        ' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService} 
        ' 
        ServicesToRun = New System.ServiceProcess.ServiceBase() {New TriMechPDM} 

        System.ServiceProcess.ServiceBase.Run(ServicesToRun) 
    End Sub 

    'Required by the Component Designer 
    Private components As System.ComponentModel.IContainer 

    ' NOTE: The following procedure is required by the Component Designer 
    ' It can be modified using the Component Designer. 
    ' Do not modify it using the code editor. 
    Friend WithEvents ProjectRequestWatcher As System.IO.FileSystemWatcher 
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() 
        Me.ProjectRequestWatcher = New System.IO.FileSystemWatcher 
        CType(Me.ProjectRequestWatcher, System.ComponentModel.ISupportInitialize).BeginInit() 
        ' 
        'ProjectRequestWatcher 
        ' 
        Me.ProjectRequestWatcher.EnableRaisingEvents = True 
        Me.ProjectRequestWatcher.Filter = "*.fr" 
        Me.ProjectRequestWatcher.NotifyFilter = System.IO.NotifyFilters.FileName 
        ' 
        'TriMechPDM 
        ' 
        Me.CanShutdown = True 
        Me.ServiceName = "TriMechPDM" 
        CType(Me.ProjectRequestWatcher, System.ComponentModel.ISupportInitialize).EndInit() 

    End Sub 

#End Region 


    Dim username As String = System.Configuration.ConfigurationSettings.AppSettings.Get("User") 
    Dim password As String = System.Configuration.ConfigurationSettings.AppSettings.Get("Password") 
    Dim server As String = System.Configuration.ConfigurationSettings.AppSettings.Get("Server") 
    Private TempDirectory As String = System.Configuration.ConfigurationSettings.AppSettings.Get("TempDirectory") 
    Private WatchDirectory As String = System.Configuration.ConfigurationSettings.AppSettings.Get("WatchDirectory") 
    Private ShowMessages As Boolean = System.Configuration.ConfigurationSettings.AppSettings.Get("ShowMessages") 

    Protected Overrides Sub OnStart(ByVal args() As String) 
        'Check PDMWorks login 

        EventLog.WriteEntry("Checking PDMWorks login") 

        Dim objPDM As New clsPDMWorksHandler 
        If Not objPDM.TestPDMWorks(username, password, server) Then 
            EventLog.WriteEntry("Login to PDMWorks failed") 
        Else 
            EventLog.WriteEntry("Login to PDMWorks successful") 
        End If 

        'start watching for file requests 
        ProjectRequestWatcher.Path = WatchDirectory 

    End Sub 

    Protected Overrides Sub OnStop() 
        ' Add code here to perform any tear-down necessary to stop your service. 
    End Sub 


    Private Sub ProjectRequestWatcher_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles ProjectRequestWatcher.Created 

        Try 
            EventLog.WriteEntry("Found a file request for project " & e.Name.Replace(".fr", "")) 

            Dim objXML As New XMLHandler 

            'Read the .fr file and find out the project for which the files are wanted 
            'give the file a chance to finish writing 


            Thread.Sleep(1000) 
            'now read it and find out what the target project is 
            Dim TargetProject As String = objXML.ReadProjectRequestFile(e.FullPath) 


            'now, go into PDMWorks and read the files associated with that project 
            Dim objPDMWorks As New clsPDMWorksHandler 

            Dim FileList As New DataTable 
            FileList = objPDMWorks.GetFilesinProject(TargetProject, username, password, server, TempDirectory) 

            If ShowMessages Then 
                EventLog.WriteEntry("Generated list of files in project" & e.Name.Replace(".fr", "")) 
            End If 

            If Not objPDMWorks Is Nothing Then 
                objPDMWorks = Nothing 
                'System.Runtime.InteropServices.Marshal.ReleaseComObject(objPDMWorks) 
            End If 

            'Now, call the XML object and write out the files in the project 
            If objXML.WriteFiles(TargetProject, WatchDirectory, FileList) Then 
                Debug.WriteLine("wrote files successfully") 
            End If 

            'now, delete the .fr file 
            If System.IO.File.Exists(e.FullPath) Then 
                System.IO.File.Delete(e.Name) 
            End If 

            If ShowMessages Then 
                EventLog.WriteEntry("Wrote XML file") 
            End If 
        Catch ex As Exception 
            'global 
            EventLog.WriteEntry(ex.Message, EventLogEntryType.Error) 
        End Try 

         
    End Sub 

End Class