![.net filewatcher stops working on network share .net filewatcher stops working on network share](https://s11921.pcdn.co/wp-content/uploads/2-xenapp-delivery-controller.png)
- #.net filewatcher stops working on network share update#
- #.net filewatcher stops working on network share code#
' Ultimately, some code can be added to insure this property is not set to True if there is no Path ' or Filter (filename) set. Also, setting ' it to false will prevent the FSW from firing during program shutdown.
![.net filewatcher stops working on network share .net filewatcher stops working on network share](https://codemade.net/images/badkeyboard.png)
Set to true only after the path and filter ' have been set through class properties to prevent premature triggering of the FSW. The class will not be destroyed until IsProcessing = False Public ReadOnly Property IsProcessingFile() As Boolean Get Return (ProcessCount > 0) End Get End Property ' Used for initializing and shutting down the process. DetailCount = 0 End With Do Try Using sr As StreamReader = New StreamReader(f.FullName) Dim InputLine As String Do Until sr.EndOfStream InputLine = sr.ReadLine() If InputLine.StartsWith(HeaderIdentifier) Then RecordCountResults.HeaderCount += 1 Else RecordCountResults.DetailCount += 1 End If Loop sr.Close() End Using Exit Do Catch ex As System.IO.IOException If Counter Nothing OnFileAdded(Me, New FileSystemEventArgs(WatcherChangeTypes.Created, SourceFilePath, ExistingFileName)) ExistingFileName = Dir() Loop End Sub ' Used primarily for shutdown. Private Function GetRecordCounts(ByVal HeaderIdentifier As String, ByVal f As FileInfo) As RecordCountResultsStructure Dim RecordCountResults As RecordCountResultsStructure Dim Counter As Integer = 0 With RecordCountResults. In that case, wait 15-seconds and try again. If an exception occurs attempting ' to open the file, it is likely because the file is still being streamed to the directory by the ' host process. Dim f As FileInfo = New FileInfo(e.FullPath) d.BeginInvoke(HeaderIdentifier, f, New AsyncCallback(AddressOf GetRecordCountsCallBack), f) End Sub ' Open the new file and get record counts (header and detail).
#.net filewatcher stops working on network share update#
Imports System.IO Public Class ImportFile Public Structure RecordCountResultsStructure Public HeaderCount As Integer Public DetailCount As Integer End Structure Public EventArgs As FileProcessedEventArgs Public Delegate Sub NewFileProcessedEventHandler(ByVal sender As Object, ByVal e As FileProcessedEventArgs) Public Event NewFileProcessedEvent As NewFileProcessedEventHandler Private FileWatcher As New FileSystemWatcher() Private IsEnabled As Boolean = False Private ProcessCount As Int16 = 0 Private ImportStatus As FileProcessedEventArgs.ImportStatuses Private ImportFileGroup As Long = 0 Private DestinationFilePath As String = String.Empty Private FileNameMask As String = String.Empty Private HeaderIdentifier As String = String.Empty Private SourceFilePath As String = String.Empty Private Delegate Function GetRecordCountsDelegate(ByVal HeaderIdentifier As String, ByVal f As FileInfo) As RecordCountResultsStructure Private d As New GetRecordCountsDelegate(AddressOf GetRecordCounts) ' FSW event fires when a new file is created in the watched directory Protected Sub OnFileAdded(ByVal source As Object, ByVal e As FileSystemEventArgs) ProcessCount += 1 Debug.WriteLine("Processes: " + ProcessCount.ToString + ", Processing " + e.FullPath) ' Get file stats, update database, then move file ' If anything fails before MoveFile(), the file will stay in the source directory. The file that does not process may or may not be the same as last time.Ĭan anyone see anything in this code that would prevent the OnFileAdded() event to just stop firing when a new file is placed in the folder? Also, once a specific event stop firing, it is 'permanent' until I stop the program and restart it. Days 3-4 the same thing, then on day 5, another file fails to process. On day 2, all but one will get processed. There is no pattern as to which one will fail to fire, or when. Currently, that consists of 8 classes that get instantiated.Ĭonsistently, at some point, one or more of these classes fails to raise an event when a new file appears in the watched folder. In the base code I instantiate one of these classes for each node. the base code reads and processes an XML file containing nodes that identify properties. I have the following class that contains a FileSystemWatcher.