C# - Monitoring folder and files

If you want to get a notification whenever a folder or any files is changed, .Net Framework provides a class name FileSystemWatcher to ease your coding. It provides an event for any files that is modified, created or deleted.

FileSystemWatcher fileWatcher = new FileSystemWatcher();
fileWatcher.Path = "C:\Data";
fileWatcher.Filter = "*.txt";
fileWatcher.IncludeSubdirectories = false;
fileWatcher.NotifyFilter = NotifyFilters.LastWrite | NotifyFilters.FileName;
fileWatcher.Changed += new FileSystemEventHandler(OnFileChanged);

The 'Filter' property is used to set what type of file being monitor. If you want to monitor all file types, just change it to "*.*" or you can specify other file type like "*.jpeg", "*.wmv" or others. The 'IncludeSubdirectories' as the name suggested is set to either include the sub directory in FileSystemWatcher monitoring or ignore it. The 'NotifyFilter' is used with Changed event, when the file attribute like last write or file name is change, the Changed event will be invoke. There is also some other event for this class as shown below:

// When a file is created in the folder
fileWatcher.Created += new FileSystemEventHandler(OnFileModified);
// When a file is deleted from the folder
fileWatcher.Deleted += new FileSystemEventHandler(OnFileModified);
// When a file in the folder is renamed
fileWatcher.Renamed += new FileSystemEventHandler(OnFileModified);

fileWatcher.EnableRaisingEvents = true;

private void OnFileModified(object sender, FileSystemEventArgs e)
    if (e.ChangeType == WatcherChangeTypes.Created || e.ChangeType == WatcherChangeTypes.Deleted)

You must set the 'EnableRaisingEvents' as true to start the FileSystemWatcher.


C# - Simple SQL command

Here are some simple code in C# if you want to select data from your database.

DataTable dataTable = new DataTable();
string connStr = "Data Source=MachineName\SQLEXPRESS;Initial Catalog=DatabaseName;Integrated Security=True";
string query = "select Id, ProductName, Description from Product";
using (SqlConnection conn = new SqlConnection(connStr))
    SqlDataAdapter da = new SqlDataAdapter(query, conn);

foreach (DataRow row in dataTable.Rows)
    Console.WriteLine(row["Id"].ToString(), row["ProductName"].ToString(), row["Description"].ToString());

Data Source is the name of the database server. Initial Catalog is the database name. If you set the Integrated Security to true, it will used the Windows account of the current process to login to the server. In case the user id and password is different from the windows account, use this

string connStr = "Data Source=MachineName\SQLEXPRESS;Initial Catalog=DatabaseName;User ID=username;Password=yourPassword";

You can also use SqlCommand to read data from the database. For other SQL command like insert, delete and update, I'm not sure if there is other way beside using SqlCommand.

using (SqlConnection conn = new SqlConnection(connStr))
    SqlCommand dataCommand = new SqlCommand();
    dataCommand.Connection = conn;
    dataCommand.CommandText = "update Product set ProductName='NewProductName' where Id='30'";

    int x = dataCommand.ExecuteNonQuery();