I want to monitor the activities of all the folders present at "C:\Inetpub\ftproot\san".User can work on any type of files and not only text files.Since we have given 1GB space (lets say) to each user, so user can do anything to utilize this space.

Variable "id" is redirected to this page from previous page and according to this id the respective folder of user is opened in windows explorer.
Now I want to monitor the activites that the user will do in his folder like creating new file, deleting an existing file or editing a file.I want to monitor user's activities because i have to keep track of the space given to the user so tht i can restrict the user to use 1GB space only and not more than that.

I got this code over the internet and found it relevant to my work.

How and where to observe the functionality of this code.I m using ASP.NET and its a web based application.

Many Thanks

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;

public partial class _Default : System.Web.UI.Page
    DAL conn;
    string connection;
    string id = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
        connection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\project\\Desktop\\BE prj\\dbsan.mdb;Persist Security Info=False";
        conn = new DAL(connection);
        ////*** Opening Respective Folder of a User ***////
        DirectoryInfo directories = new DirectoryInfo(@"C:\\Inetpub\\ftproot\\san\\");
        //DirectoryInfo directories = new DirectoryInfo(@"C:\");
        DirectoryInfo[] folderList = directories.GetDirectories();
        if (Request.QueryString["id"] != null)
            id = Request.QueryString["id"];

        string path = Path.Combine(@"C:\\Inetpub\\ftproot\\san\\", id);

        int folder_count = folderList.Length;
        for (int j = 0; j < folder_count; j++)
            if (Convert.ToString(folderList[j]) == id)
                Process p = new Process();
                p.StartInfo.FileName = path;


        ClsFileSystemWatcher FSysWatcher = new ClsFileSystemWatcher();

    public class ClsFileSystemWatcher

        public void FileWatcher(string InputDir)
            using (FileSystemWatcher fsw = new FileSystemWatcher())
                fsw.Path = InputDir;
                fsw.Filter = @"*";
                fsw.IncludeSubdirectories = true;

                fsw.NotifyFilter =
                    NotifyFilters.FileName |
                    NotifyFilters.Attributes |
                    NotifyFilters.LastAccess |
                    NotifyFilters.LastWrite |
                    NotifyFilters.Security |
                    NotifyFilters.Size |
                    NotifyFilters.CreationTime |

                fsw.Changed += new FileSystemEventHandler(OnChanged);
                fsw.Created += new FileSystemEventHandler(OnCreated);
                fsw.Deleted += new FileSystemEventHandler(OnDeleted);
                fsw.Renamed += new RenamedEventHandler(OnRenamed);
                fsw.Error += new ErrorEventHandler(OnError);

                fsw.EnableRaisingEvents = true;

                //string strOldFile = InputDir + "OldFile.txt";
                //string strNewFile = InputDir + "CreatedFile.txt";

                //// Making changes in existing file

                //using (FileStream stream = File.Open(strOldFile, FileMode.Append))
                //    StreamWriter sw = new StreamWriter(stream);
                //    sw.Write("Appending new line in Old File");
                //    sw.Flush();
                //    sw.Close();


                //// Writing new file on FileSystem

                //using (FileStream stream = File.Create(strNewFile))
                //    StreamWriter sw = new StreamWriter(stream);
                //    sw.Write("Writing First line into the File");
                //    sw.Flush();
                //    sw.Close();


                // Minimum time given to event handler to track new events raised by the filesystem.


        public static void OnChanged(object source, FileSystemEventArgs e)
            Console.WriteLine("File " + e.FullPath + " :" + e.ChangeType);

        public static void OnDeleted(object source, FileSystemEventArgs e)
            Console.WriteLine("File " + e.FullPath + " :" + e.ChangeType);

        public static void OnCreated(object source, FileSystemEventArgs e)
            Console.WriteLine("File " + e.FullPath + " :" + e.ChangeType);

        public static void OnRenamed(object source, RenamedEventArgs e)
            Console.WriteLine("File " + e.OldFullPath + " [Changed to] " + e.FullPath);

        public static void OnError(object source, ErrorEventArgs e)
            Console.WriteLine("Error " + e.ToString());


I don't think its feasible to run FileSystemWatcher from a web application. All the callback methods are gone once the page finished processing a web request. The FileSystemWatcher will continue to run in a separate process after your page finished, but will not be able to call any of your code because the page class instance is destroyed and it may be unloaded from memory. I think that the code you posted was intended to be run under windows environment, not web. (Even event handlers write to Console.)

I think your best bet is to write a windows service that uses FileSystemWatcher to monitor folders and keeps all required info in a database. Then your ASP.NET applications can read/write values from/to the database to communicate with the windows service.

YA the same thing is happening when we run the above code.

Got ur point and I m searching for it myself too but is it possible to integrate WindowsApplication with WebApplication?? if yes then how.


You can integrate a windows app with web app via a data store, such as the database.

Say, for example, you create a database with a table that stores the user's name, the amount of space allocated to that user, and the amount of space used by that user. This data will be updated by the windows app. Now, if your web app needs to display how much space a user is using, it can simply read the data for that user from the database.

In this scenario, only the windows app can update this data, while web app can only read it. Of course, there could be some data items that need to be read by windows app or written by both. That decision should be based on what the data is and how it is inteded to be used. For example, the web app may decide to increase the space allocated to a user (because the user paid for it over a web form), and the windows app will read that data whenever it needs to know the max allowed size.

Thanx ... the thing is all clear but can u provide any link(coding in C#) which uses FileSystemWatcher on windowsapplication, storing in a DataBase and webapplication retrives it ???
Is there any class analogous to FileSystemWatcher which directly supports WebApplication (ASP.NET) ????