Skip to main content

File System Watcher

The Following is the example for File System Watcher.

Create Console Application in Visual Studio.
File --> New --> Project
In the Template Select the Console Application.

Eg:


Namespaces Used:

using System.Security.Permissions;
using System.IO;

These two namespaces used for file operation and file permissions.
Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Permissions;
using System.IO;

namespace Watcher
{
    public class Watcher
    {
         public static void Main()
        {
            Run();
        }
        [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
        public static void Run()
        {
            // Create a new FileSystemWatcher and set its properties.
            FileSystemWatcher watcher = new FileSystemWatcher();
            watcher.Path = @"D:\Pilot\Sam\Projects\Watcher";

            /* Watch for changes in the renaming of files. */
            watcher.NotifyFilter = NotifyFilters.FileName;
            watcher.IncludeSubdirectories = false;

            // Only watch txt files. This property is optional
            watcher.Filter = "*.txt";

            // Add event handlers.
            watcher.Created += new FileSystemEventHandler(OnCreated);

            // Begin watching.
            watcher.EnableRaisingEvents = true;

            // Wait for the user to quit the program.
            Console.WriteLine("Press \'q\' to quit the Watcher.");
            while (Console.Read() != 'q') ;
        }

        // Define the event handlers.
        private static void OnCreated(object source, FileSystemEventArgs e)
        {
            // Specify what is done when a file is created.
            Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);

            try
            {
                StreamReader reader = File.OpenText(e.FullPath);
                string line = null;

                while ((line = reader.ReadLine()) != null)
                {
                    Console.Write(line + Environment.NewLine);
                }
            }
            catch (Exception ex)
            {
                Console.Clear();
                Console.WriteLine(ex);
            }
        }
    }
}

 Run the Console application.


It watches the  path "D:\Pilot\Sam\Projects\Watcher".


Steps to watch the output.
  1. Create a text file with some content.
  2. Copy that file and paste it to your project folder.
  3. Then watch the output in Watcher.

Output:

Uses:
  1. We can upload the excel (.xls) file format via web. In the web server, the upload watcher is being run always and watching at the particular folder path.
  2. If any upload happens via web it can be saved in that watcher path.
  3. Once file uploaded, the upload watcher catches that file and performs the database actions.



Comments

Popular posts from this blog

SQL - Reporting Server - Part III

Display Reports in Web Application Open Visual studio and create a new Web Application. Add Microsoft Report Viewer control. Specify the ReportServerURL and ReportPath in report properties. Run the application and see the output in browser.

SQL - Reporting Server - Part II

Create a New Report Server Project 1. Open Visual Studio. 2. New --> Project -->    2.1. Select Project type --> Business Intelligence Project.    2.2. From the template select   Report Server Project. 3. In the solution, Right click in Shared Datasource and select  Add New Datasource. 4. In the properties window general tab enter the data source name and server type.    4.1. Create new connection click Edit button.    4.2. Or you can directly enter the connection string a in the text box. And In the Credentials tab enter the username and password.  Eg.  Data Source=SAM;Initial Catalog=NHG; Adding New Reports:    You can add the report in two ways.   1. Using Report Wizard.   2. Using Bland Report.   1. Using Report Wizard. 1.1. In the solution, Right Click at Reports and select Add New Report. It will display the Report Wizard to gene...

SQL - Reporting Server - Part I

Configure a Reporting Server in SQL - 2008 Steps: 1. Open the Reporting Service Configuration Manager. Start --> All Programs --> Microsoft SQL Server 2008 --> Configuration Tools --> Reporting Services Configuration Manager. 2. Select the server name from the list. 3. Set the server account name 4. Set the Service URL. 5.Select or create Reportserver database. 6. 6. Input the virtual directory path. 7. Now we successfully configured the Reporting Server.