Problem in FileSystem Wather usign C#.net

Posted by Naraayanan under C# on 3/12/2012 | Points: 10 | Views : 1126 | Status : [Member] | Replies : 0
Hi friends,
In my Project, I am using Backgroundworker ,FileSystemwather ,some button and Some TextBox. Some times, Filesystem Watcher is not triggered.Please help me.Please see my Code.

Declaration Part:
  FileSystemWatcher fswatcher = new FileSystemWatcher();


Coding Part:
From Button , To button adn From TextBox ,To TextBox using FolderBrowserDialog.
private void btn_From_Click(object sender, EventArgs e)
{
Fbrowsedialog = new FolderBrowserDialog();
Fbrowsedialog.ShowDialog();
txtfrom.Text = Fbrowsedialog.SelectedPath;


}

private void btn_To_Click(object sender, EventArgs e)
{
Fbrowsedialog = new FolderBrowserDialog();
Fbrowsedialog.ShowDialog();
txtTo.Text = Fbrowsedialog.SelectedPath;
}

Copy Files:
btn_Copy Event
  private void btn_copy_Click(object sender, EventArgs e)
{
btn_From.Enabled = false;
btn_To.Enabled = false;
btn_copy.Enabled = false;
bworker.RunWorkerAsync();
}


Using Background Component

private void bworker_DoWork(object sender, DoWorkEventArgs e)
{
try
{
DirectoryInfo dir = new DirectoryInfo(txtfrom.Text);
string[] files = Directory.GetFiles(txtfrom.Text);
if (files.Length > 0)
{
chkDir(txtfrom.Text, txtTo.Text, "pcl");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}

private void bworker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{

}

private void bworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
try
{
btn_From.Enabled = true;
btn_To.Enabled = true;
btn_copy.Enabled = true;

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Function for Copying One folder and Another Folder with Partcular file Type:

 private static void chkDir(string fileDir, string sPath, string fext)
{
string[] fsDir = Directory.GetFiles(fileDir);
foreach (string f in fsDir)
{
string fName = f.Substring(fileDir.Length + 1);
string[] fileExt = fName.Split('.');
if (fileExt[1] == fext)
{
File.Copy(Path.Combine(fileDir, fName), Path.Combine(sPath, fName), true);
}
}

}

Created Event for FileSystem Watcher
  private void watcher_Created(object source, FileSystemEventArgs e)
{
MessageBox.Show("Files created");
}


Monitoring btn
      private void btn_fmonitor_Click(object sender, EventArgs e)
{
fswatcher.Path = txtTo.Text;
fswatcher.Filter = "*.pcl";
fswatcher.Created += new FileSystemEventHandler(watcher_Created);
fswatcher.EnableRaisingEvents = true;
fswatcher.Created += new FileSystemEventHandler(watcher_Created);

}


Close button
 private void btn_close_Click(object sender, EventArgs e)
{
fswatcher.EnableRaisingEvents = false;
Application.Exit();
}


File Created .But message Box will not display. why? Please tell me.

Regards,
Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/
http://abaprocker.blogspot.com/



Responses

(No response found.)

Login to post response