title: how to use FileSystemWatcher using asp.net(C#)
body: <p>SENARIO: i have few folders on my FTP server, belongs to particular user. Suppose i have 10GB total space and i assign 1GB to each user i.e can accomodate 10 users having 1GB each. </p>
  
  <p>now users can add/delete/edit any type of file to utilize the storage space. All i need to do is to restrict users not to exceed 1gb space for their file storage. For this i want to use FileSystemWatcher to notify me that a user had created/deleted/edited a file so that i can minimize the space from 1gb incase of creation of a file or add a space incase of deletion. </p>
  
  <p>this is the piece of coding using FSW. when user gets loged-in with proper id and password, respective folder is opened (present at FTP server) where he can add/delete/edit any type of file and according to that i hav to monitor d space ulitilized by him.</p>
  
  <p>but d problem is the event handlers (written in console). i dont understand what happens when this code is being runned... i dontknow how to use FSW class so that i can monitor d changes user is making in his folder.</p>
  
  <p>please help ... THANX</p>
  
  <pre><code>using System;
  using System.Diagnostics;
  using System.IO;
  using System.Threading;
  
  public class _Default: System.Web.UI.Page {
      public class ClsFileSystemWatcher {
          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);
          }
  
          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|NotifyFilters.DirectoryName;
                  fsw.Changed += OnChanged;
                  fsw.Created += OnCreated;
                  fsw.Deleted += OnDeleted;
                  fsw.Renamed += OnRenamed;
                  fsw.Error += 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();     
                  //}    
                  //File.Delete(strOldFile);     
                  //File.Delete(strNewFile);     
                  // Minimum time given to event handler to track new events raised by the filesystem.    
                  Thread.Sleep(1000);
              }
          }
      }
  
      private DAL conn;
      private string connection;
      private 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[] 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 &lt; folder_count; j++) {
              if (Convert.ToString(folderList[j]) == id) {
                  Process p = new Process();
                  p.StartInfo.FileName = path;
                  p.Start();
              }
          }
          ClsFileSystemWatcher FSysWatcher = new ClsFileSystemWatcher();
          FSysWatcher.FileWatcher(path);
      }
  }
  </code></pre>