Newbie question!---Is this possible?

  • Thread starter Thread starter nuB
  • Start date Start date
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
 
Hi,

I suggest you to narrow the problem and post minimal code to reproduce the
problem, because posting too much code and vague information about the exact
error discourage people to try to reproduce your problem.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
Thanks for the advice. Here is the problem I'm having now. I'm trying
to write lines to a text file as the program runs through each step. It
works up to the line marked with "***"

'begin logging
Dim pLogPath As String = (archiveFolderPath &
"\billTransfer.log")
Dim pLogFile As New FileStream(pLogPath, FileMode.Create,
FileAccess.Write)
Dim pLog As New StreamWriter(pLogFile)
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(" ") 'stops writing here

Dim pvbills As String
pvbills = ("d:\EXPORT\pvbills.dat")
Dim pvfinal As String
pvfinal = ("d:\EXPORT\pvfinal.dat")
Dim activeFilename As String
activeFilename = ("PWA" & myMonth & myDay & "1.IN.1")
Dim inactiveFilename As String
inactiveFilename = ("PWA" & myMonth & myDay & "2.IN.1")
Dim activePath As String
activePath = (archiveFolderPath & "\" & activeFilename)
Dim inactivePath As String
inactivePath = (archiveFolderPath & "\" & inactiveFilename)
File.Move(pvbills, activePath)
File.Move(pvfinal, inactivePath)

pLog.WriteLine("Finished renaming and moving files...")
pLog.WriteLine(pvbills & "was moved to " & archiveFolderPath &
"\" & activeFilename)
pLog.WriteLine(pvfinal & "was moved to " & archiveFolderPath &
"\" & inactiveFilename)
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("")
pLog.Close()

What am I doing wrong?
 
The line

pLog.WriteLine(" ") 'stops writing here

is simple and correct and it should not fail. I guess that you think that is
failing in that line because the file only contains text up to that line? If
so, I recommend to use during debug pLog.Flush() after each plog.WriteLine
call to actually flush the buffer and get the actual line that is causing
the problem.

Also, are you using exception handlers around your code?:

Try
your code
Catch ex as Exception
' Log to somewhere (event log, send e-mail)
End Try

If an exception happens and you are not catching it, the service will stop
working silently.

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
Excellent. I will use the exception handler example you gave me to try
and find the problem. Is there a way to send information on the part of
the program that caused the error to the event log?
 
If I use pLog.Close() after the line I marked, it will write everything
to that point. However, the code sample I posted produces an empty text
file.
 
See System.Diagnostics.EventLog class, and be very careful writing the
logging code because if the messenger of an error fails, you will miss the
error...

--

Best regards,

Carlos J. Quintero

MZ-Tools: Productivity add-ins for Visual Studio .NET, VB6, VB5 and VBA
You can code, design and document much faster.
Free resources for add-in developers:
http://www.mztools.com
 
I found the problem. I needed to check the existence of the files I was
moving using an IF statement before I moved them. It was giving an
error that it couldn't find the files. I will check out the EventLog
class. Thanks for your help. I know it's probably not much fun
discussing such rudimentary programming concepts. Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top