【问题标题】:how to use FileSystemWatcher using asp.net(C#)如何使用 asp.net(C#) 使用 FileSystemWatcher
【发布时间】:2010-06-27 14:54:28
【问题描述】:

SENARIO:我的 FTP 服务器上的文件夹很少,属于特定用户。假设我有 10GB 的总空间,我为每个用户分配 1GB,即可以容纳 10 个用户,每个用户有 1GB。

现在用户可以添加/删除/编辑任何类型的文件以利用存储空间。我需要做的就是限制用户的文件存储空间不超过 1gb。为此,我想使用 FileSystemWatcher 通知我用户已经创建/删除/编辑了一个文件,以便我可以在创建文件的情况下最小化 1gb 的空间或在删除的情况下添加空间。

这是一段使用 FSW 的编码。当用户使用正确的 ID 和密码登录时,会打开相应的文件夹(存在于 FTP 服务器上),他可以在其中添加/删除/编辑任何类型的文件,据此我必须监控他使用的空间。

但问题是事件处理程序(写在控制台中)。我不明白运行此代码时会发生什么...我不知道如何使用 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);
    }
}

【问题讨论】:

标签: asp.net


【解决方案1】:

每次重新加载页面时,您都会创建新的 FSW - 在这种情况下,您不会收到任何事件,因为从新创建的 FSW 开始,没有任何变化。尝试将 FileSystemWatcher 对象保留在 Session 状态。

所以流程看起来像:

  • 用户登录 - 您创建 FSW 并将其保存在 Session 中
  • 用户重新加载页面 – 从 Session 中获取 FSW(不要创建新的)

【讨论】:

  • 如果你不介意那么你可以重新编码我的编码,如果你认为这对我很有帮助。请..谢谢
【解决方案2】:

您应该为这种类型的事物创建一个工作角色(服务)。我认为在页面内包含这样的内容是不合适的。

【讨论】:

  • 但问题是如何创建服务... ???任何示例编码都将受到高度赞赏thanx
  • 是的服务更适合这里,但恕我直言,您可以在 ASP.NET 中使用 FSW
  • CodeProject 的一篇老文章 - 如何在 C# 中实现一个简单的 filewatcher Windows 服务 - codeproject.com/KB/files/C__FileWatcher.aspx
猜你喜欢
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多