N
nuB
I am in the process of developing a service that will monitor a folder
for the creation of two files. When the files are created, I would like
the service to do the following:
Rename the files
Create an archive directory
Move these files to the archive directory
Create a log in the same dir to log the process
FTP the files to an external FTP server
Send a confirmation email with the log to a user
I would also like an icon to be displayed in the taskbar while the
service is running.
Here is what I have so far. It is not working and I'm not sure why. Any
help would be greaatly appreciated!
Imports System.ServiceProcess
Imports System.IO
Imports System.Diagnostics
Public Class Service1
Inherits System.ServiceProcess.ServiceBase
Public folderToWatch As FileSystemWatcher
#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
Service1}
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.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container()
Me.ServiceName = "Service1"
End Sub
#End Region
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set
things
' in motion so your service can do its work.
Dim strWatchedDir As String
folderToWatch = New FileSystemWatcher
strWatchedDir = "d:\EXPORT"
folderToWatch.Path = strWatchedDir
With folderToWatch
.NotifyFilter = .NotifyFilter Or _
NotifyFilters.FileName
.NotifyFilter = .NotifyFilter Or _
NotifyFilters.Attributes
.NotifyFilter = .NotifyFilter Or _
NotifyFilters.CreationTime
.IncludeSubdirectories = False
End With
AddHandler folderToWatch.Created, AddressOf procWatcherObj
folderToWatch.EnableRaisingEvents = True
End Sub
Private Sub procWatcherObj(ByVal Source As Object, ByVal evt As
FileSystemEventArgs)
If evt.ChangeType = WatcherChangeTypes.Created Then
procBillFiles(evt.FullPath, evt.Name)
End If
End Sub
Private Sub procBillFiles(ByVal filePath As String, ByVal fileName
As String)
If fileName = "pvbills.dat" Or "pvfinal.dat" Then
'create base folder in archive directory
Dim archiveFolderPath As String
Dim myTimestamp As String
Dim myMonth As String
Dim myDay As String
Dim myYear As String
Dim dateFName As String
myMonth = Month(myTimestamp)
myDay = Day(myTimestamp)
myYear = Year(myTimestamp)
dateFName = (myMonth & myDay & myYear)
archiveFolderPath = ("d:\EXPORT\ARCHIVE\" & dateFName)
Dim myDir As DirectoryInfo =
Directory.CreateDirectory(archiveFolderPath)
'create log file to begin logging events
Dim pLogPath As String = (archiveFolderPath & "log.txt")
Dim pLog As StreamWriter = New StreamWriter(pLogPath)
'begin logging
pLog.WriteLine("Begin Arista transfer process log: ")
pLog.WriteLine(Now())
pLog.WriteLine(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
pLog.WriteLine(" ")
'rename data files and move to archive dir
pLog.WriteLine("Renaming and moving files...")
pLog.WriteLine(Hour(Now) & ":" & Minute(Now) & ":" &
Second(Now))
pLog.WriteLine("")
Dim activeFilename As String
Dim activePath As String
Dim inactiveFileName As String
Dim inactivePath As String
Dim pvbills As String
Dim pvfinal As String
pvbills = ("d:\EXPORT\pvbills.dat")
pvfinal = ("d:\EXPORT\pvfinal.dat")
activeFilename = ("PWA" & myMonth & myDay & "1.IN.1")
activePath = (archiveFolderPath & activeFilename)
inactiveFileName = ("PWA" & myMonth & myDay & "2.IN.1")
inactivePath = (archiveFolderPath & inactiveFileName)
File.Move(pvbills, activePath)
File.Move(pvfinal, inactivePath)
pLog.WriteLine("Finished renaming and moving files...")
pLog.WriteLine(Hour(Now) & ":" & Minute(Now) & ":" &
Second(Now))
pLog.WriteLine("")
'send files to FTP server
pLog.WriteLine("Start FTP file transfer...")
pLog.WriteLine(Hour(Now) & ":" & Minute(Now) & ":" &
Second(Now))
pLog.WriteLine("")
Dim ff As clsFTP
Dim fileList As String
ff = New clsFTP("MY FTP SERVER", MY DIR", "MY USER", "MY
PWD", 80)
If (ff.Login() = True) Then
pLog.WriteLine("Log on successful...")
ff.UploadFile(activePath)
pLog.WriteLine("File " & activePath & " copied to FTP
server...")
ff.UploadFile(inactivePath)
pLog.WriteLine("File " & inactivePath & " copied to FTP
server...")
'verify file upload
End If
If ff.flag_bool = True Then
ff.CloseConnection()
End If
End If
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.
folderToWatch.EnableRaisingEvents = False
End Sub
End Class
for the creation of two files. When the files are created, I would like
the service to do the following:
Rename the files
Create an archive directory
Move these files to the archive directory
Create a log in the same dir to log the process
FTP the files to an external FTP server
Send a confirmation email with the log to a user
I would also like an icon to be displayed in the taskbar while the
service is running.
Here is what I have so far. It is not working and I'm not sure why. Any
help would be greaatly appreciated!
Imports System.ServiceProcess
Imports System.IO
Imports System.Diagnostics
Public Class Service1
Inherits System.ServiceProcess.ServiceBase
Public folderToWatch As FileSystemWatcher
#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
Service1}
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.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container()
Me.ServiceName = "Service1"
End Sub
#End Region
Protected Overrides Sub OnStart(ByVal args() As String)
' Add code here to start your service. This method should set
things
' in motion so your service can do its work.
Dim strWatchedDir As String
folderToWatch = New FileSystemWatcher
strWatchedDir = "d:\EXPORT"
folderToWatch.Path = strWatchedDir
With folderToWatch
.NotifyFilter = .NotifyFilter Or _
NotifyFilters.FileName
.NotifyFilter = .NotifyFilter Or _
NotifyFilters.Attributes
.NotifyFilter = .NotifyFilter Or _
NotifyFilters.CreationTime
.IncludeSubdirectories = False
End With
AddHandler folderToWatch.Created, AddressOf procWatcherObj
folderToWatch.EnableRaisingEvents = True
End Sub
Private Sub procWatcherObj(ByVal Source As Object, ByVal evt As
FileSystemEventArgs)
If evt.ChangeType = WatcherChangeTypes.Created Then
procBillFiles(evt.FullPath, evt.Name)
End If
End Sub
Private Sub procBillFiles(ByVal filePath As String, ByVal fileName
As String)
If fileName = "pvbills.dat" Or "pvfinal.dat" Then
'create base folder in archive directory
Dim archiveFolderPath As String
Dim myTimestamp As String
Dim myMonth As String
Dim myDay As String
Dim myYear As String
Dim dateFName As String
myMonth = Month(myTimestamp)
myDay = Day(myTimestamp)
myYear = Year(myTimestamp)
dateFName = (myMonth & myDay & myYear)
archiveFolderPath = ("d:\EXPORT\ARCHIVE\" & dateFName)
Dim myDir As DirectoryInfo =
Directory.CreateDirectory(archiveFolderPath)
'create log file to begin logging events
Dim pLogPath As String = (archiveFolderPath & "log.txt")
Dim pLog As StreamWriter = New StreamWriter(pLogPath)
'begin logging
pLog.WriteLine("Begin Arista transfer process log: ")
pLog.WriteLine(Now())
pLog.WriteLine(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
pLog.WriteLine(" ")
'rename data files and move to archive dir
pLog.WriteLine("Renaming and moving files...")
pLog.WriteLine(Hour(Now) & ":" & Minute(Now) & ":" &
Second(Now))
pLog.WriteLine("")
Dim activeFilename As String
Dim activePath As String
Dim inactiveFileName As String
Dim inactivePath As String
Dim pvbills As String
Dim pvfinal As String
pvbills = ("d:\EXPORT\pvbills.dat")
pvfinal = ("d:\EXPORT\pvfinal.dat")
activeFilename = ("PWA" & myMonth & myDay & "1.IN.1")
activePath = (archiveFolderPath & activeFilename)
inactiveFileName = ("PWA" & myMonth & myDay & "2.IN.1")
inactivePath = (archiveFolderPath & inactiveFileName)
File.Move(pvbills, activePath)
File.Move(pvfinal, inactivePath)
pLog.WriteLine("Finished renaming and moving files...")
pLog.WriteLine(Hour(Now) & ":" & Minute(Now) & ":" &
Second(Now))
pLog.WriteLine("")
'send files to FTP server
pLog.WriteLine("Start FTP file transfer...")
pLog.WriteLine(Hour(Now) & ":" & Minute(Now) & ":" &
Second(Now))
pLog.WriteLine("")
Dim ff As clsFTP
Dim fileList As String
ff = New clsFTP("MY FTP SERVER", MY DIR", "MY USER", "MY
PWD", 80)
If (ff.Login() = True) Then
pLog.WriteLine("Log on successful...")
ff.UploadFile(activePath)
pLog.WriteLine("File " & activePath & " copied to FTP
server...")
ff.UploadFile(inactivePath)
pLog.WriteLine("File " & inactivePath & " copied to FTP
server...")
'verify file upload
End If
If ff.flag_bool = True Then
ff.CloseConnection()
End If
End If
End Sub
Protected Overrides Sub OnStop()
' Add code here to perform any tear-down necessary to stop your
service.
folderToWatch.EnableRaisingEvents = False
End Sub
End Class