VB.NET

Moderators: seancampbell
Number of threads: 4020
Number of posts: 10026

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
FileSystemWatcher instances pointing to single method Posted by pr0grammer on 29 Aug 2008 at 8:01 PM
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" ?>
<configuration>
<appSettings>
<add key="aih" value="..."/>
<add key="aim" value="..."/>
<add key="atich" value="..."/>
<add key="aticm" value="..."/>
<add key="muh" value="..."/>
<add key="mum" value="..."/>
<add key="lmc" value="..."/>
<add key="nci" value="..."/>
<add key="united" value="..."/>
</appSettings>
</configuration>

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??



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.