2010-06-27 28 views
0

SENARIO:我的FTP服務器上有幾個文件夾,屬於特定用戶。假設我有10GB的總空間,我爲每個用戶分配1GB,即可容納10個每個1GB的用戶。如何使用FileSystemWatcher使用asp.net(C#)

現在用戶可以添加/刪除/編輯任何類型的文件來利用存儲空間。我所需要做的就是限制用戶不要超過1GB空間來存儲文件。爲此,我想使用FileSystemWatcher通知我,用戶已經創建/刪除/編輯了一個文件,這樣我就可以最小化1gb創建文件的空間,或者增加刪除空間。

這是使用FSW的編碼。當用戶使用正確的ID和密碼進入時,打開各個文件夾(出現在FTP服務器上),他可以在其中添加/刪除/編輯任何類型的文件,並根據該文件監視由他製作的d空間。

但問題是事件處理程序(寫在控制檯中)。我不明白當這段代碼正在運行時會發生什麼...我不知道如何使用FSW類,以便我可以監視用戶在他的文件夾中進行的更改。

請幫助... THANX

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 < 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); 
    } 
} 
+0

如何使用NTFS磁盤配額? http://technet.microsoft.com/en-us/library/cc781892(WS.10).aspx – Lucero 2010-06-27 15:03:10

回答

3

每次重裝你創建新的FSW頁時間 - 在這種情況下,你不會得到任何事件引發,因爲從新建FSW什麼點是變化。嘗試保留會話狀態下的FileSystemWatcher對象。

所以流看起來像:在

  • 用戶登錄 - 創建FSW和會話維護它
  • 用戶重新加載頁面 - 從會議獲得FSW(不創建新)
+0

如果你不介意,那麼你可以重新編碼我的代碼,如果你認爲這可以工作,對我來說這將是很大的幫助。 plz .. THANX – ariez88 2010-06-29 06:53:54

0

您應該爲這種類型的事情創建一個輔助角色(服務)。我認爲在頁面內部放置這樣的東西是不合適的。

+0

但問題是如何創建服務... ???任何示例代碼將高度讚賞thanx – ariez88 2010-06-29 06:54:59

+0

是的服務會更適合這裏,但恕我直言,你可以在ASP.NET中使用FSW – saku 2010-06-29 11:41:03

+0

來自CodeProject的一篇舊文章 - 如何在C#中實現一個簡單的filewatcher Windows服務 - http://www.codeproject .COM/KB /文件/ C__FileWatcher.aspx – tathagata 2010-06-29 12:37:44