Click here to Skip to main content
Licence CPOL
First Posted 27 Oct 2007
Views 23,492
Downloads 97
Bookmarked 18 times

Automatically process incoming files

By | 27 Oct 2007 | Article
Automatically process incoming files by using the DirectoryInfo class instead of FileSystemWatcher.

Introduction

Hello all. This is a little article on how to process incoming data automatically. In the short past, I've seen lots of people trying to use the FileSystemWatcher to trigger the data processing function in their software. However, many of them have to deal with the exception: "The process cannot access the file because it is being used by another process". We can fix this problem very easily. We only have to create a thread which reads the directory constantly, using the DirectoryInfo class. Next to that, we create a ProcessFile function, which processes the file and moves it into the 'processed' directory.

Using the code

Here we go:

private bool ProgramRunning = true;
private int FilesHandled = 1;

static void Main(string[] args)
{
    Program prog = new Program();
    prog.ReadDirectoryThread();
}

I use the field FilesHandled to count the number of files I've processed, just so that I can verify whether all files have been processed or not.

private void ReadDirectoryThread()
{
    while (this.ProgramRunning)
    {
        DirectoryInfo DirInfo = new DirectoryInfo(
            Path.GetDirectoryName(
            Process.GetCurrentProcess().MainModule.FileName)
            + "\\incoming\\");
        foreach (FileInfo FInfo in DirInfo.GetFiles("*.xml"))
        {
            Thread t = new Thread(new ParameterizedThreadStart(ProcessFile));
            t.Start(FInfo.FullName);
            while (t.ThreadState != System.Threading.ThreadState.Stopped)
            {
                //Thread.Sleep(5);
            }
        }
    }
}

We continuously check if new files are created. With the GetFiles() function, you can set a filter on a specific document type, for example, XML files. As soon as the directory contains documents, the ProcessFile function is called with the ParameterizedThreadStart() function. You can add a Thread.Sleep() in the t.ThreadState while loop to decrease the CPU usage; however, it is not recommended when you have to process a high amount of data files constantly.

void ProcessFile(object FileToProcess)
{
    string sFile = FileToProcess.ToString();
    bool isProcessed = false;
    // File processing
    try
    {
        // Process your file here.
    }
    catch
    {
        return;
    }
    // After the file is processed, we move it
    while (!isProcessed)
    {
        try
        {
            string ProcessedFilesDir = string.Format("{0}\\processed\\{1}_{2}.xml",
                Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName),
                DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss"), Guid.NewGuid().ToString());
            File.Move(sFile, ProcessedFilesDir);
            Console.WriteLine(FilesHandled + " - File " + 
                              sFile.ToString() + " processed\n");
            FilesHandled++;
        }
        catch
        {
            isProcessed = false;
        }
        finally
        {
            isProcessed = true;
        }
    }
}

In the first part in the code above, we can call the function to handle our data file, e.g., an import function or whatsoever. After the data has been handled successfully, we try to move the file to the 'processed' directory. In case the file has exclusive rights, the thread keeps trying to move the file until it's finally moved.

I've had a few cases where more than a hundred data files came in every second. Therefore, I added the DateTime.Now.ToString("dd_MM_yyyy_HH_mm_ss") and the Guid.NewGuid().ToString() functions, to create a proper filename for the processed file, without any risks on exceptions.

Because this is a console application, I used Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName). However, in Windows Forms, you can use Application.StartupPath instead.

History

  • 27 Oct 2007 - First version published.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Eric de Maar

Software Developer
WDM
Netherlands Netherlands

Member

Programmer from Groningen, the Netherlands.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
General100% CPU Usage! Pinmemberalien70006:48 18 Feb '09  
GeneralRe: 100% CPU Usage! Pinmemberalien70007:03 18 Feb '09  
GeneralHelfpul! Pinmemberleonvd20:11 2 Nov '07  
QuestionHow I have done this in the past. PinmemberBohicette210:57 30 Oct '07  
QuestionWhat is wrong with the FileMpnitor class PinmemberJoe Sonderegger21:18 28 Oct '07  
AnswerRe: What is wrong with the FileMpnitor class PinmemberEric de Maar22:31 28 Oct '07  
GeneralRe: What is wrong with the FileMpnitor class PinmemberJoe Sonderegger2:17 29 Oct '07  
GeneralRe: What is wrong with the FileMpnitor class PinmemberNico W3:16 29 Oct '07  
GeneralRe: What is wrong with the FileMpnitor class PinmemberJoe Sonderegger3:31 29 Oct '07  
GeneralRe: What is wrong with the FileMpnitor class PinmemberNico W3:45 29 Oct '07  
GeneralRe: What is wrong with the FileMpnitor class PinmemberJoe Sonderegger3:51 29 Oct '07  
GeneralRe: What is wrong with the FileMpnitor class PinmemberNico W4:10 29 Oct '07  
GeneralRe: What is wrong with the FileMpnitor class PinmemberJoe Sonderegger5:28 29 Oct '07  
GeneralRe: What is wrong with the FileMpnitor class PinmemberNico W5:47 29 Oct '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 27 Oct 2007
Article Copyright 2007 by Eric de Maar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid