2010-06-27 3 views
0

SENARIO : FTP 서버에 폴더가 거의없고 특정 사용자에게 속합니다. 총 공간이 10GB이고 각 사용자에게 1GB를 할당한다고 가정합니다. 즉 각 사용자가 1GB 인 10 명의 사용자를 수용 할 수 있습니다.asp.net (C#)을 사용하여 FileSystemWatcher를 사용하는 방법

사용자는 저장 공간을 활용하기 위해 모든 유형의 파일을 추가/삭제/편집 할 수 있습니다. 내가해야 할 일은 파일 저장을 위해 1 기가 바이트 공간을 초과하지 않도록 사용자를 제한하는 것입니다. 이것을 위해 FileSystemWatcher를 사용하여 사용자가 파일을 생성/삭제/편집 할 수 있도록 파일을 만들 때 1GB의 공간을 최소화하거나 삭제 공간을 추가 할 수 있도록 파일을 작성/삭제/편집했음을 알리려고합니다.

이것은 FSW를 사용하는 코딩 조각입니다. 사용자가 적절한 ID 및 암호로 로그인하면 각 폴더가 열리고 (FTP 서버에 있음) 모든 유형의 파일을 추가/삭제/편집 할 수 있으며 그에 따라 i가 ulitilized 된 공간을 모니터링 할 수 있습니다.

하지만 문제는 콘솔에서 작성된 이벤트 핸들러입니다. 나는이 코드가 실행될 때 어떤 일이 일어나는지 이해하지 못한다 ... 나는 FSW 클래스를 사용하여 사용자가 그의 폴더에서 변경 한 내용을 모니터링 할 수있는 방법을 모르겠다.

도와주세요 ... 고맙습니다

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

답변

3

당신은 당신이 새로운 FSW를 만들 페이지 다시로드 할 때마다 - 때문에 새로 만든 FSW 아무것도의 관점에서, 어떤 이벤트가 발생되지 않습니다이 경우였다 변경. FileSystemWatcher 개체를 Session 상태로 유지하십시오.

그래서 흐름과 같습니다에

  • 사용자 로그 - 세션에서 FSW를 얻을 수 (새로 생성하지 않는)
-
  • 사용자가 페이지를 다시로드 당신은 FSW를 생성하고 세션에 보존
  • +0

    만약 당신이 내 생각을하면 큰 도움이 될 것입니다 내 코딩 코드를 recode 수 없다면 이것이 작동 할 수 있습니다. plz .. 고맙습니다 – ariez88

    0

    이 유형의 작업자 역할 (서비스)을 만들어야합니다. 페이지의 내부에 이와 같은 것을 갖는 것이 적절하지 않다고 생각합니다.

    +0

    하지만 질문은 어떻게 서비스를 만드는 것입니다 ... ??? 모든 예제 코드는 매우 높게 평가 될 것입니다 – ariez88

    +0

    그래 서비스가 여기에 더 적합 할 것이지만 IMHO에서는 ASP.NET에서 FSW를 사용할 수 있습니다 – saku

    +0

    CodeProject의 오래된 기사 - C#에서 간단한 파일 공유기 Windows 서비스를 구현하는 방법 - http : //www.codeproject .com/KB/파일/C__FileWatcher.aspx – tathagata

    관련 문제