EDN Admin
Well-known member
Ok, this proves that i still need work in learning c#...anyway I am trying to create a method that will retrieve a list of directories from a table in a database, then create a file watcher to monitor these directories. My objective is to get this code to
monitor multiple directories. So here is my challenge...with the code I have below, I want the first method to return a list of directories, then use that list in the second method to get my watcher started. I cant seem to get these two methods to talk the
way i want them to...So basically i want the first method to return the list of directories to watch, then pass that to the second method which would then loop through them...can someone please help? What am I doing wrong? Thanks for your patience and help.
<div style="color:Black;background-color:White; <pre>
FileSystemWatcher fileSystemWatcher = <span style="color:Blue; new FileSystemWatcher();
ArrayList aFileWatcherInstance = <span style="color:Blue; new ArrayList();
List<<span style="color:Blue; string> dirMonitored = <span style="color:Blue; new List<<span style="color:Blue; string>();
<span style="color:Blue; private List<<span style="color:Blue; string> RetrieveFoldersToMonitor()<br/>{
<span style="color:Blue; string query = <span style="color:#A31515; "SELECT column FROM myTable";
<span style="color:Blue; string connectionString = <span style="color:#A31515; "Data Source=mySvr;Initial Catalog=myDB;Integrated Security=True";
<span style="color:Blue; using (SqlConnection conn = <span style="color:Blue; new SqlConnection(connectionString))
{
conn.Open();
<span style="color:Blue; using (SqlCommand myCommand = <span style="color:Blue; new SqlCommand(query, conn))
{
<span style="color:Blue; using (SqlDataReader myReader = myCommand.ExecuteReader())
{
<span style="color:Blue; while (myReader.Read())
{
dirMonitored.Add(myReader.GetString(0));
}
}
}
conn.Close();
}
}
<span style="color:Blue; private <span style="color:Blue; void CreateWatchers(List<<span style="color:Blue; string> path)<br/>{
<span style="color:Blue; foreach (<span style="color:Blue; string dir <span style="color:Blue; in path)
{
<span style="color:Blue; if (Directory.Exists(dir))
{
<span style="color:Blue; this.fileSystemWatcher.Filter = <span style="color:#A31515; "*";
<span style="color:Blue; this.fileSystemWatcher.Path = dir;
<span style="color:Blue; this.fileSystemWatcher.IncludeSubdirectories = <span style="color:Blue; false;
fileSystemWatcher.Changed += <span style="color:Blue; new FileSystemEventHandler(fileSystemWatcher_Changed);
fileSystemWatcher.Created += <span style="color:Blue; new FileSystemEventHandler(fileSystemWatcher_Created);
aFileWatcherInstance.Add(fileSystemWatcher);
}
}
}
[/code]
View the full article
monitor multiple directories. So here is my challenge...with the code I have below, I want the first method to return a list of directories, then use that list in the second method to get my watcher started. I cant seem to get these two methods to talk the
way i want them to...So basically i want the first method to return the list of directories to watch, then pass that to the second method which would then loop through them...can someone please help? What am I doing wrong? Thanks for your patience and help.
<div style="color:Black;background-color:White; <pre>
FileSystemWatcher fileSystemWatcher = <span style="color:Blue; new FileSystemWatcher();
ArrayList aFileWatcherInstance = <span style="color:Blue; new ArrayList();
List<<span style="color:Blue; string> dirMonitored = <span style="color:Blue; new List<<span style="color:Blue; string>();
<span style="color:Blue; private List<<span style="color:Blue; string> RetrieveFoldersToMonitor()<br/>{
<span style="color:Blue; string query = <span style="color:#A31515; "SELECT column FROM myTable";
<span style="color:Blue; string connectionString = <span style="color:#A31515; "Data Source=mySvr;Initial Catalog=myDB;Integrated Security=True";
<span style="color:Blue; using (SqlConnection conn = <span style="color:Blue; new SqlConnection(connectionString))
{
conn.Open();
<span style="color:Blue; using (SqlCommand myCommand = <span style="color:Blue; new SqlCommand(query, conn))
{
<span style="color:Blue; using (SqlDataReader myReader = myCommand.ExecuteReader())
{
<span style="color:Blue; while (myReader.Read())
{
dirMonitored.Add(myReader.GetString(0));
}
}
}
conn.Close();
}
}
<span style="color:Blue; private <span style="color:Blue; void CreateWatchers(List<<span style="color:Blue; string> path)<br/>{
<span style="color:Blue; foreach (<span style="color:Blue; string dir <span style="color:Blue; in path)
{
<span style="color:Blue; if (Directory.Exists(dir))
{
<span style="color:Blue; this.fileSystemWatcher.Filter = <span style="color:#A31515; "*";
<span style="color:Blue; this.fileSystemWatcher.Path = dir;
<span style="color:Blue; this.fileSystemWatcher.IncludeSubdirectories = <span style="color:Blue; false;
fileSystemWatcher.Changed += <span style="color:Blue; new FileSystemEventHandler(fileSystemWatcher_Changed);
fileSystemWatcher.Created += <span style="color:Blue; new FileSystemEventHandler(fileSystemWatcher_Created);
aFileWatcherInstance.Add(fileSystemWatcher);
}
}
}
[/code]
View the full article