Vijay
Vijay

Reputation: 383

Alternatives to using FileSystemWatcher in Web Applications

I use Forms authentication in my ASP.NET web application and I use FileSystemWatcher in a specific form. The functionality of the form is as follows.
1. The user logs in to the application.
2. The application should keep scanning a folder for any new files (XML) added. The path to the folder is read from the database.
3. When a new file is created in the folder, the application reads the relevant data from the XML file and displays information regarding that file in the form.

After searching for relevant controls to achieve this functionality, I went for FileSystemWatcher. I am not quite sure how effective it is in web applications. Can anyone suggest alternatives to FileSystemWatcher in ASP.NET web applications?

Upvotes: 4

Views: 6503

Answers (5)

JamieSee
JamieSee

Reputation: 13030

Why over complicate things? Just store a last polled date and then get any files that have a modified date greater than the last polled date. FileSystemWatcher has a number of caveats to its use and you don't really need it for what you're doing.

With DirectoryInfo and a little 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;
}

Upvotes: 4

Kapil Khandelwal
Kapil Khandelwal

Reputation: 16144

Have a look at this:

This article shows how FileSystemWatcher can be used within web applications, and then go on to describe the Cache mechanism that provides an ASP.NET specific alternative.

Extending FileSystemWatcher to ASP.NET

Upvotes: 0

ADIMO
ADIMO

Reputation: 1117

If you want create a real time notification, you must develop two components: a service that use FileSystemWatcher and check new events, from web side, instead, you must use SignaIR for build real-time, multi-user interactive web applications.

Upvotes: 0

Mr. TA
Mr. TA

Reputation: 5359

The FileSystemWatcher is an appropriate class, however, using it from the form is not the right manner. Instead, try creating a background worker, either in your ASP.NET application (less desirable) or in a separate Windows service (more desirable), and your form would then communicate with that worker using an appropriate mechanism. The worker would then scan the folder and read the data and make it ready to be consumable by your form. That mechanism could be a set of events and shared variables in the case of a worker thread in the ASP.NET app, or a WCF named pipes channel for a Windows Service.

Upvotes: 1

jrummell
jrummell

Reputation: 43117

I'm not aware of an ASP.NET equivalent to FileSystemWatcher.

However, you could add a Begin_Request event to Global.asax to check for new files on each request.

Upvotes: 0

Related Questions