views:

24

answers:

0

This is utterly baffling to me.

I have a service with an array of file system watchers, the path and filter of each set at runtime.

Protected Overrides Sub OnStart(ByVal args() As String)
    Dim l As String = ConfigurationManager.AppSettings("dirs")
    Dim x As String = ConfigurationManager.AppSettings("extensions")
    Dim locs() As String = l.Split("|")
    Dim extensions() As String = x.Split("|")
    For i As Integer = 0 To locs.Length - 1
        Dim aLocation As String = locs(i)
        Dim anExtension As String = extensions(i)

        Dim fw As New FileSystemWatcher
        fw.Path = aLocation
        fw.EnableRaisingEvents = True
        fw.Filter = anExtension

        AddHandler fw.Created, AddressOf fileCreated
        watchers.Add(fw)
    Next

End Sub


Public Sub fileCreated(ByVal source As Object, ByVal e As FileSystemEventArgs)

    Dim theDir2 As DirectoryInfo = New DirectoryInfo(e.FullPath.TrimEnd(e.Name.ToCharArray))
    Dim targetExtension As String = New FileInfo(e.FullPath).Extension
    Dim theDir As String = e.FullPath.TrimEnd(e.Name.ToCharArray)

    Try
        check(theDir, targetExtension.ToLower)
    Catch ex As Exception
        'gotta get something in here someday

    End Try
End Sub

The first half of the problem is that regardless of the value I have in the "extensions" key in app.config - say "*.TXT" or "*.txt" - the created event only fires when a targeted file with its extension in lowercase comes in.

The second half of the problem is that for reasons beyond my control, another process is tossing in the files I want, with .TXT as the extension.

I'm pulling my hair out here. Also it's a pain to work on because on my development machine, XP with up to date service packs, it works just fine - txt and TXT equally trigger the created event.

Does anyone have any tricks or tips on dealing with this? I need the created event to fire regardless of the file extension casing.

Edit

Well, I just made it more hacky and more ugly, but it works. If anyone comes along and has encountered the same problem, here's what I did.

I changed the filter for the dirs that might end up with uppercase file extensions to /., and then added two extra keys to the appconfig. You'll see them referenced in the code below. Some days I feel more like a ship breaker than I do a programmer.

Public Sub fileCreated(ByVal source As Object, ByVal e As FileSystemEventArgs)

    Dim theDir2 As DirectoryInfo = New DirectoryInfo(e.FullPath.TrimEnd(e.Name.ToCharArray))
    Dim targetExtension As String = New FileInfo(e.FullPath).Extension
    Dim theDir As String = e.FullPath.TrimEnd(e.Name.ToCharArray)
    If CType(source, FileSystemWatcher).Filter = "*.*" Then
        Dim paths() As String = ConfigurationManager.AppSettings("oddBallDirs").Split("|")
        Dim extensions() As String = ConfigurationManager.AppSettings("oddBallExtensions").Split("|")
        Dim myPath As String = CType(source, FileSystemWatcher).Path
        Dim myRealExtension As String = String.Empty

        For i As Integer = 0 To paths.Count - 1
            If paths(i).ToUpper = myPath.ToUpper Then
                myRealExtension = extensions(i).Replace("*", "")
                targetExtension = myRealExtension
            End If
        Next
    End If

    Try
        check(theDir, targetExtension.ToLower)
    Catch ex As Exception
        'gotta get something in here someday

    End Try
End Sub