FileSystemWatcher in C#

Hi all,

Here is the simple description to use FileSystemWatcher in your desktop application by using C#.

Just drag the FileSystemWatcher control in your form from under the Components tab in the toolbox.

And copy the following code in to load event,

private void Form1_Load(object sender, EventArgs e)
{
fileSystemWatcher.Path = flvPath;
fileSystemWatcher.Filter = "*.flv";
fileSystemWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime | NotifyFilters.LastWrite;
fileSystemWatcher.EnableRaisingEvents = true;
}

If the file watcher found any newly created file, the following event will rise.

private void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
MessageBox.Show(e.Name + " created in " + e.FullPath);
}


An event is rising when a file deleted.

private void fileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
{
MessageBox.Show(e.Name + " changed in " + e.FullPath);
}

An event is rising when a file renamed.


private void fileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
{
MessageBox.Show(e.Name + " renamed in " + e.FullPath);
}

That’s it…

...S.VinothkumaR.

How to generate an xml from TreeView using C#?

Hi guys,

Here is the sample code for generating xml file from TreeView. I needed this for one of my project. I just referred lot of sites and finally came to conclusion with the following code. Just copy and paste this code and use it for yours.

using System.IO;

private static StreamWriter sr;

private void btnExportXML_Click(object sender, EventArgs e)
{
if (tvXML.Nodes.Count > 0) // Here tvXML is TreeView control
{
sr = new StreamWriter(path); // Here path is xml path
sr.Write("<" + tvXML.Nodes[0].Text + ">");
parseNode(tvXML.Nodes[0]);
sr.Close();
}
}

//Use the following method for getting value of each and every node

private static void parseNode(TreeNode tn)
{
IEnumerator ie = tn.Nodes.GetEnumerator();
string parentnode = "";
parentnode = tn.Text;
while (ie.MoveNext())
{
TreeNode ctn = (TreeNode)ie.Current;
if (ctn.GetNodeCount(true) == 0)
{
if (ctn.Tag != null)
{
sr.Write("<" + ctn.Text + ">");
sr.Write(ctn.Tag);
sr.Write("");
}
else
{
sr.Write("<" + ctn.Text + "/>");
}
}
else
{
sr.Write("<" + ctn.Text + ">");
}
if (ctn.GetNodeCount(true) > 0)
{
parseNode(ctn);
}
}
sr.Write("");
sr.WriteLine("");
}

...S.VinothkumaR.

How to bind Countries in a combobox using c#?

Find the code below to bind countries in a combo box using C#.


public void BindCountries()
{
foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures))
{
RegionInfo ri = new RegionInfo(ci.LCID);
comboBox1.Items.Add(ri.EnglishName);
}
comboBox1.Sorted = true;
comboBox1.SelectedIndex = 0;
}

...S.VinothkumaR.