.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

Problem in FileSystem Watcher Event in C#.net

Posted By: Narayanan     Posted Date: March 12, 2012    Points:5   Category :C#
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.

Responses
Author: Rajesh             
Posted Date: March 12, 2012     Points: 20   

hi try this code
private void bworker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
try
{
MessageBox.Show("file create sucess")
btn_From.Enabled = true;
btn_To.Enabled = true;
btn_copy.Enabled = true;


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


Author: Narayanan             
Posted Date: March 12, 2012     Points: 20   

Hi Rajesh,
Thanks for your reply. But i want to conform file is created or not via filesystem watcher.So Please tell me .

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


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend