FileSystemWatcher Help?

K

Kiran B

I am working on filesystemwatcher and use to watch a folder to see if any
new files have been created. If any new file is created then, i will open it
parse it and save the parsed data into db. It works fine when the file is
created for first time (Say a.x12 file is created on folder new and it
works)Problem I am having is, when i put a second (Say b.x12) file to that
folder, it says can't open the file because the process is being used. It
works fine if i don't open and parse it meaning if i don't process the file.
It let me put as many as file i want on that folder and it notifies me...but
as soon as i open that parse function it craps out. any clue..

I have attached a code: thanks

Public Sub startup()

myDBCls.DBConn = conStr

' Create a watcher

_watcher = New FileSystemWatcher(TrackFolder)

' Create an event handeler

AddHandler _watcher.Created, AddressOf Me.ProcessTracking



'Start Watching

_watcher.Filter = "*.x12"

_watcher.EnableRaisingEvents = True

' Tell the user

Log("Monitoring '" & TrackFolder & "'")

End Sub

' Stop This method

Public Sub ShutDown()

If Not myDBCls Is Nothing Then

myDBCls = Nothing

End If

' stop the watcher

If Not _watcher Is Nothing Then

_watcher.EnableRaisingEvents = False

_watcher = Nothing

Log("Watcher Closed")

End If

End Sub

' Write log info

Public Sub Log(ByVal buf As String)

Console.WriteLine(Date.Now.ToLongTimeString & ": " & buf)

End Sub

' ProcessTracking

Public Sub ProcessTracking(ByVal sender As Object, _

ByVal e As FileSystemEventArgs)

' Tell the user

Log("Processing '" & e.FullPath & "'....")

' we'll do the processing here

'Dim rslt As String

Dim str As String

str = ProcessTracking(e.FullPath)

' tell the processing

'Log("Finished '" & e.FullPath & "'.....")

'Log("Deleting File '" & e.FullPath & "'.....")

End Sub

Public Function ProcessTracking(ByVal fileName As String) As String

' open file to read

Dim output() As String

Dim copyline As Boolean = False

Dim count As Integer

Dim sep As String

Dim s As String

Dim arrTrack()

Dim ponumber As String

Dim i As Integer

Dim strSQL As String

Dim sr As StreamReader

'Dim strd As Stream = File.OpenRead(fileName)

sr = New StreamReader(fileName) '*********** THis is where it craps out
when i put second file in that folder

'Dim sr As StreamReader = New StreamReader(strd)

' set the file pointer at the begining

sr.BaseStream.Seek(0, SeekOrigin.Begin)

count = 0

While (sr.Peek() > -1)

'ProcessLine(sr.ReadLine)

copyline = False

sep = "~"

s = sr.ReadLine()

output = Split(s, sep, -1, 1)

'If sr.AtEndOfStream Then

'Exit Do

'Else

If output(0) = "REF" And output(1) = "AW" Then

count = count + 1

ReDim Preserve arrTrack(count)

arrTrack(count - 1) = output(2)

'arrTrack(count) = output(2)

Log("UB " & UBound(arrTrack) & "<br>")

'Response.Write count & "<br>"

ElseIf output(0) = "PRF" Then

ponumber = output(1)

ElseIf output(0) = "SE" Then

copyline = True

End If

If copyline Then

Log("Store In DB" & "<br>")

Log("storing..." & "<br>")

For i = 0 To UBound(arrTrack) - 1

strSQL = "INSERT into synnexTrackInfo values('','" & ponumber & "','" &
arrTrack(i) & "',convert(char(24),getdate(),112),'','SYN','')"

Log(strSQL)

Log("Stored Procedure")

'myDBCls.StoreTracking(ponumber, arrTrack(i), "SYN")

'objconnection.Execute(strSQL)

Next

Erase arrTrack

count = 0

End If

'End If

End While

sr.Close()

sr = Nothing

End Function
 
N

Nico Debeuckelaere

What I do when I get a file created event is starting a new thread that
pauses for a little while and then does all the rest.
I think this has to do that the system gets the new file event but the file
is not written completely yet.

Regards,

--

Nico Debeuckelaere

ND-Sign BVBA (Microsoft Certified Partner since 2004)
URL: http://www.nd-sign.com
== ND-Sign, Designed for you ==
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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

Top