【问题标题】:Alternatives to using FileSystemWatcher in Web Applications在 Web 应用程序中使用 FileSystemWatcher 的替代方法
【发布时间】:2012-06-11 14:31:37
【问题描述】:

我在 ASP.NET Web 应用程序中使用表单身份验证,并在特定表单中使用 FileSystemWatcher。表单的功能如下。
1。用户登录到应用程序。
2。应用程序应继续扫描文件夹以查找添加的任何新文件 (XML)。文件夹的路径是从数据库中读取的。
3。在文件夹中创建新文件时,应用程序从 XML 文件中读取相关数据并在表单中显示有关该文件的信息。

在搜索了实现此功能的相关控件后,我选择了 FileSystemWatcher。我不太确定它在 Web 应用程序中的效果如何。谁能建议 ASP.NET Web 应用程序中 FileSystemWatcher 的替代方案?

【问题讨论】:

    标签: c# asp.net filesystemwatcher


    【解决方案1】:

    为什么要把事情复杂化?只需存储最后轮询日期,然后获取修改日期大于最后轮询日期的任何文件。 FileSystemWatcher 对其使用有许多注意事项,您实际上并不需要它。

    使用 DirectoryInfo 和一点 Linq:

    public static string[] GetNewFiles(string directory, string searchPattern, DateTime since)
    {
        // Create a new DirectoryInfo object.
        DirectoryInfo dir = new DirectoryInfo(directory);
    
        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException("The directory does not exist.");
        }
    
        // Call the GetFileSystemInfos method.
        FileSystemInfo[] infos = dir.GetFileSystemInfos(searchPattern);
    
        string[] newXmlFiles = (from info in infos
                               where info.CreationTime > since
                               select info.FullName).ToArray();
    
        return newXmlFiles;
    }
    
    public static string[] GetNewOrUpdatedFiles(string directory, string searchPattern, DateTime since)
    {
        // Create a new DirectoryInfo object.
        DirectoryInfo dir = new DirectoryInfo(directory);
    
        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException("The directory does not exist.");
        }
    
        // Call the GetFileSystemInfos method.
        FileSystemInfo[] infos = dir.GetFileSystemInfos(searchPattern);
    
        string[] newXmlFiles = (from info in infos
                               where info.LastWriteTime > since
                               select info.FullName).ToArray();
    
        return newXmlFiles;
    }
    
    public static string[] GetUpdatedFiles(string directory, string searchPattern, DateTime since)
    {
        // Create a new DirectoryInfo object.
        DirectoryInfo dir = new DirectoryInfo(directory);
    
        if (!dir.Exists)
        {
            throw new DirectoryNotFoundException("The directory does not exist.");
        }
    
        // Call the GetFileSystemInfos method.
        FileSystemInfo[] infos = dir.GetFileSystemInfos(searchPattern);
    
        string[] newXmlFiles = (from info in infos
                               where info.LastWriteTime > info.CreationTime && info.LastWriteTime > since
                               select info.FullName).ToArray();
    
        return newXmlFiles;
    }
    

    【讨论】:

    • JamieSee,我喜欢你的简单方法,它很有效,所以 +1 来自我
    【解决方案2】:

    FileSystemWatcher 是一个合适的类,但是,从表单中使用它不是正确的方式。相反,尝试在您的 ASP.NET 应用程序(不太理想)或单独的 Windows 服务(更理想)中创建一个后台工作程序,然后您的表单将使用适当的机制与该工作程序通信。然后,工作人员将扫描文件夹并读取数据并准备好供您的表单使用。对于 ASP.NET 应用程序的工作线程或 Windows 服务的 WCF 命名管道通道,该机制可能是一组事件和共享变量。

    【讨论】:

      【解决方案3】:

      我不知道与 FileSystemWatcher 等效的 ASP.NET。

      但是,您可以将Begin_Request 事件添加到 Global.asax 以检查每个请求的新文件。

      【讨论】:

        【解决方案4】:

        如果你想创建一个实时通知,你必须开发两个组件:一个使用 FileSystemWatcher 并从 web 端检查新事件的服务,相反,你必须使用SignaIR 来构建实时、多用户交互网络应用程序。

        【讨论】:

          【解决方案5】:

          看看这个:

          本文展示了如何在 Web 应用程序中使用 FileSystemWatcher,然后继续描述提供 ASP.NET 特定替代方案的缓存机制。

          Extending FileSystemWatcher to ASP.NET

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2021-08-28
            • 1970-01-01
            • 1970-01-01
            • 2011-11-11
            • 2016-09-28
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多