Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

FileSystemWatcher instances pointing to single method

pr0grammerpr0grammer Posts: 1Member
I'm creating an application with SystemFileWatcher objects to monitor multiple folders that are created on a SQL Server folder.

the files are created by a trigger on the respective server.

the application has the following example entries on the app.config file.

<?xml version="1.0" encoding="utf-8" ?>














my current source is a follows:

Imports System.Xml
Imports System.Text
Imports System.IO
Imports System.Configuration
Imports System.Collections.Specialized
Imports System.Collections.ObjectModel
Imports System.Collections
Imports System.Reflection
Imports System.Diagnostics
Imports System.Runtime.InteropServices
Imports System.Data.SqlClient
Imports Microsoft.SqlServer.Management.Smo

Public Class Form1
Dim m_Proj As CRAXDRT.Application ' Create a new COM instance of the Crystal Reports
Dim WithEvents m_Report As CRAXDRT.Report ' The dynamically generated report

Dim textbox As TextBox
Dim lbl As Label
Dim grpBox As GroupBox
Dim chkBox As CheckBox
Dim fsw As FileSystemWatcher
Dim afsw As New ArrayList()

ReadOnly Property ServerName(ByVal strText As String) As String
Get
Return strText.Substring(strText.IndexOf("=") + 1, strText.IndexOf(";Initial Catalog") - strText.IndexOf("=") - 1)
End Get
End Property

ReadOnly Property DBName(ByVal strText As String) As String
Get
Return strText.Substring(strText.IndexOf("=", strText.IndexOf(";")) + 1, strText.IndexOf(";uid") - 1 - strText.IndexOf("=", strText.IndexOf(";") + 1))
End Get
End Property

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
For i As Integer = afsw.Count - 1 To 0 Step -1
afsw.RemoveAt(i)
Next

For i As Integer = Controls.Count - 1 To 0 Step -1
Controls.RemoveAt(i)
Next
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For i As Integer = 0 To ConfigurationManager.AppSettings.Count - 1

grpBox = New GroupBox
grpBox.Location = New System.Drawing.Point(IIf(i Mod 2, 400, 50), IIf(i Mod 2, ((i - 1) * 40) + 8, (i * 40) + 8))
grpBox.Size = New System.Drawing.Size(283, 70)
grpBox.Name = "txt" + ConfigurationManager.AppSettings.GetKey(i)
grpBox.Text = ConfigurationManager.AppSettings.GetKey(i).ToUpper
grpBox.BackColor = Color.Wheat


chkBox = New System.Windows.Forms.CheckBox
chkBox.Size = New System.Drawing.Size(100, 20)
chkBox.Checked = True
chkBox.Text = "Running"
chkBox.Location = New System.Drawing.Point(20, 15)
chkBox.Name = "chk" + ConfigurationManager.AppSettings.GetKey(i) & "_" & i.ToString
AddHandler chkBox.CheckStateChanged, AddressOf chkChangeProcessing
grpBox.Controls.Add(chkBox)

fsw = New FileSystemWatcher("\stg" & ServerName(ConfigurationManager.AppSettings(i)) & "c$sqlwatch" & DBName(ConfigurationManager.AppSettings(i)), "*.txt")
fsw.EnableRaisingEvents = True
fsw.IncludeSubdirectories = False
fsw.NotifyFilter = NotifyFilters.FileName Or NotifyFilters.LastWrite Or NotifyFilters.CreationTime
AddHandler fsw.Created, AddressOf fswCreateImage
ProcessExistingFiles(fsw, fsw.Path)
afsw.Add(fsw)
Next
End Sub

Public Sub chkChangeProcessing(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim chkBox As CheckBox
Dim i As Integer
Dim fsw As FileSystemWatcher
Try
chkBox = CType(sender, CheckBox)

chkBox.Text = (IIf(chkBox.CheckState, "Running", "Stopped"))
i = CInt(chkBox.Name.Substring(chkBox.Name.IndexOf("_") + 1, chkBox.Name.Length - chkBox.Name.IndexOf("_") - 1))

fsw = CType(afsw(i), FileSystemWatcher)
Select Case chkBox.Text
Case "Running"
fsw.EnableRaisingEvents = True
ProcessExistingFiles(fsw, fsw.Path)
Case "Stopped"
fsw.EnableRaisingEvents = False
End Select
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Sub

' Process existing files.
Private Sub ProcessExistingFiles(ByVal fsw As FileSystemWatcher, ByVal path As String)
Dim files As String() = Directory.GetFiles(path, "*.txt")
For Each s As String In files
fswCreateImage(fsw, New System.IO.FileSystemEventArgs(WatcherChangeTypes.All, "", s))
Next
End Sub

Public Sub fswCreateImage(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs)
Dim fsw As FileSystemWatcher
fsw = CType(sender, FileSystemWatcher)

File.Delete(e.FullPath)
End Sub
End Class

i am not sure if the method "fswCreateImage" can handle the multiple calls from the instances of FileSystemWatcher given the Created events that can occur from the files created by the trigger.

any suggestions??
Sign In or Register to comment.