I want to insert file information into a database for all files created in a directory. My program will do that if I drop small numbers of files in the directory but it doesn't get them all if I drop larger amounts in the directory all at once (I didn't do a whole lot of testing but it only inserted about 200 names into my database when I tried to drop 800 files in the directory at once).
Here's my code:
static void Main(string[] args)
{
// Create a new FileSystemWatcher and set its properties.
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\dropDirectory";
// Add event handlers.
watcher.Created += new FileSystemEventHandler(OnChanged);
// Begin watching.
watcher.EnableRaisingEvents = true;
while(DateTime.Now.Hour < 10);
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
string strInsert = "INSERT INTO Files (Filename) VALUES ('" + e.Name + "')";
string strConnection = "Server = server_name; Database = database_name; User Id = user_id; Password = password;";
using (SqlConnection con = new SqlConnection(strConnection))
{
using (SqlCommand cmd = new SqlCommand(strInsert, con))
{
con.Open();
cmd.ExecuteScalar();
}
}
}
What changes do I need to make so my OnChanged method will be called for every file dropped in the destination directory regardless of the number of files dropped at once? Thanks in advance.