I'm in *desperate* need for help. I'm a bit of a newbie, so please be gentle. 
I'm trying write a vb.net program as a service. Essentially, all I want to do is have the FileSystemWatcher wait for files to be copied into a directory. As they are, any that have a .bmp, a .pcl, or a .inv extension should be moved to two different directories - one is on a different server. Right now, I'm copying, verifying, and deleting the original. But I have a few problems:
1. On startup of the service, I make a call to my method to copy the files. It works fine on startup. But as the service keeps running, if I copy files into the directory, the FileSystemWatcher doesn't seem to fire. Since I can't do something simple like show a messagebox, is there some way I can verify that my event is or isn't firing? Is there something I have to do different? When I created my FileSystemWatcher, I do put the name of the directory in the Path property. Is there something else I need to do? Aarrgh!
2. There is another software package that is trying to process and move these files. I want my program to pick these up and delete them before theirs. I can give a higher priority on the service, but is there a better method to move files? Since I'm copying to a different server, I can't move the files, right?
3. The way this is coded, if four files are copied into the directory, my event fires four times (regardless of how many files meet my criteria). Of course, as I said before, this is only working on startup of the service at this time. Because the event fires too many times, I try to copy the same files over and over again. I noticed that I can put a filter on the FileSystemWatcher I created, but can I list multiples? For instance "*.inv,*.bmp,*.pcl".
Here's my code:
...
Public Sub New()
MyBase.New()
moveFiles()
...
Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
moveFiles()
End Sub
Private Sub moveFiles()
Dim dt As Date = Date.Now
Dim myArray() As String
Dim i As Integer
Dim j As Integer
Dim myString As String
Dim destFile1 As String = "c:\program files\rxlaser\rxserver5 \bkandlog\"
Dim destFile2 As String = "\\rescamilla01\faxmove\"
Dim logFile As String = "c:\program files\rxlaser\rxserver5\bkandlog\faxmove" & dt.Day.ToString("00"
& dt.Month.ToString("00"
& dt.Year.ToString.Substring(2, 2) & ".txt"
Dim srcFilesLoc As String = "c:\program files\rxlaser\rxserver5\poll"
Dim fsSave As IO.FileStream
Dim srWriter As IO.StreamWriter
fsSave = New IO.FileStream(logFile, IO.FileMode.Append, IO.FileAccess.Write)
srWriter = New IO.StreamWriter(fsSave)
myArray = System.IO.Directory.GetFiles(srcFilesLoc)
For i = 0 To UBound(myArray)
If IO.Path.GetExtension(myArray(i)) = ".bmp" Or _
IO.Path.GetExtension(myArray(i)) = ".inv" Or _
IO.Path.GetExtension(myArray(i)) = ".pcl" Then
'MessageBox.Show(myArray(i))
Try
myString = dt.ToString & " File: " & myArray(i)
System.IO.File.Copy(myArray(i), destFile1 & System.IO.Path.GetFileName(myArray(i)))
System.IO.File.Copy(myArray(i), destFile2 & System.IO.Path.GetFileName(myArray(i)))
Catch ex As IO.IOException
srWriter.WriteLine(dt.ToString & " File: " & myArray(i) & " NOT copied - REASON: " & ex.Message)
Finally
myString = String.Concat(myString, " Successfully moved"
End Try
If (IO.File.Exists(destFile1 & IO.Path.GetFileName(myArray(i)))) Then And _
IO.File.Exists(destFile2 & IO.Path.GetFileName(myArray(i)))) Then 'delete original
srWriter.WriteLine(myString)
'IO.File.Delete(myArray(i))
End If
End If
Next
srWriter.Close()
fsSave.Close()
End Sub
If anyone can help me, I'd really appreciate it. It'd guarantee you a nice Christmas, Hannukah, Kwanzaa or non-holiday holiday card.
Thanks very much!
Steve
P.S. Any other general help or criticisms with my sphaghetti code would be warmly welcomed!
I'm trying write a vb.net program as a service. Essentially, all I want to do is have the FileSystemWatcher wait for files to be copied into a directory. As they are, any that have a .bmp, a .pcl, or a .inv extension should be moved to two different directories - one is on a different server. Right now, I'm copying, verifying, and deleting the original. But I have a few problems:
1. On startup of the service, I make a call to my method to copy the files. It works fine on startup. But as the service keeps running, if I copy files into the directory, the FileSystemWatcher doesn't seem to fire. Since I can't do something simple like show a messagebox, is there some way I can verify that my event is or isn't firing? Is there something I have to do different? When I created my FileSystemWatcher, I do put the name of the directory in the Path property. Is there something else I need to do? Aarrgh!
2. There is another software package that is trying to process and move these files. I want my program to pick these up and delete them before theirs. I can give a higher priority on the service, but is there a better method to move files? Since I'm copying to a different server, I can't move the files, right?
3. The way this is coded, if four files are copied into the directory, my event fires four times (regardless of how many files meet my criteria). Of course, as I said before, this is only working on startup of the service at this time. Because the event fires too many times, I try to copy the same files over and over again. I noticed that I can put a filter on the FileSystemWatcher I created, but can I list multiples? For instance "*.inv,*.bmp,*.pcl".
Here's my code:
...
Public Sub New()
MyBase.New()
moveFiles()
...
Private Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Created
moveFiles()
End Sub
Private Sub moveFiles()
Dim dt As Date = Date.Now
Dim myArray() As String
Dim i As Integer
Dim j As Integer
Dim myString As String
Dim destFile1 As String = "c:\program files\rxlaser\rxserver5 \bkandlog\"
Dim destFile2 As String = "\\rescamilla01\faxmove\"
Dim logFile As String = "c:\program files\rxlaser\rxserver5\bkandlog\faxmove" & dt.Day.ToString("00"
Dim srcFilesLoc As String = "c:\program files\rxlaser\rxserver5\poll"
Dim fsSave As IO.FileStream
Dim srWriter As IO.StreamWriter
fsSave = New IO.FileStream(logFile, IO.FileMode.Append, IO.FileAccess.Write)
srWriter = New IO.StreamWriter(fsSave)
myArray = System.IO.Directory.GetFiles(srcFilesLoc)
For i = 0 To UBound(myArray)
If IO.Path.GetExtension(myArray(i)) = ".bmp" Or _
IO.Path.GetExtension(myArray(i)) = ".inv" Or _
IO.Path.GetExtension(myArray(i)) = ".pcl" Then
'MessageBox.Show(myArray(i))
Try
myString = dt.ToString & " File: " & myArray(i)
System.IO.File.Copy(myArray(i), destFile1 & System.IO.Path.GetFileName(myArray(i)))
System.IO.File.Copy(myArray(i), destFile2 & System.IO.Path.GetFileName(myArray(i)))
Catch ex As IO.IOException
srWriter.WriteLine(dt.ToString & " File: " & myArray(i) & " NOT copied - REASON: " & ex.Message)
Finally
myString = String.Concat(myString, " Successfully moved"
End Try
If (IO.File.Exists(destFile1 & IO.Path.GetFileName(myArray(i)))) Then And _
IO.File.Exists(destFile2 & IO.Path.GetFileName(myArray(i)))) Then 'delete original
srWriter.WriteLine(myString)
'IO.File.Delete(myArray(i))
End If
End If
Next
srWriter.Close()
fsSave.Close()
End Sub
If anyone can help me, I'd really appreciate it. It'd guarantee you a nice Christmas, Hannukah, Kwanzaa or non-holiday holiday card.
Thanks very much!
Steve
P.S. Any other general help or criticisms with my sphaghetti code would be warmly welcomed!