Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 643
Vijay Avatar asked Oct 27 '25 08:10

Vijay


2 Answers

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;
}
like image 181
JamieSee Avatar answered Oct 28 '25 23:10

JamieSee


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.

like image 40
Mr. TA Avatar answered Oct 28 '25 23:10

Mr. TA