Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Help needed!!! FileSystemWatcher

Help needed!!! FileSystemWatcher

Scheduled Pinned Locked Moved C#
helpcsharpcsstutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    g3e
    wrote on last edited by
    #1

    Hi, I've written a windows service to parse files.It uses the FileSystemWatcher class in C# to sniff a folder for particular type of files. But when the files were being processed, i repeatedly get the error "The process cannot access the file "AAA" because it is being used by another process". To overcome this problem, i added the following. lines of code

    public bool ParseEnvFile(string strFilePath)
    {
    try
    {
    using(FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
    // Do some processing here
    fs.Close();
    m_nReadAttempts = 0;
    }
    }
    catch(IOException ex)
    {
    if(m_nReadAttempts < 3)
    {
    Thread.Sleep(2000);
    m_nReadAttempts++;
    return ParseEnvFile(strFilePath);
    }
    else
    {
    throw ex;
    }
    }
    .....
    }

    But even this did not solve the problem. The exception still kept coming, though less frequently. So I was forced to add a Timer event(runs every 5 mins) that would pick up all the files that were left over because of the exception. Now, my problem is this: When a file gets created in the folder, sometimes both the OnFileCreated event and the timer event try to access the file at the same time. Within my service, I'm required to open the file in write mode and update some contents. I find that at times the file that gets created ends up as a 0KB file (Note: when it initially got created the file had some content in it)!!!! Any ideas on how to solve it? Thanks for any help in resolving this problem.

    M 1 Reply Last reply
    0
    • G g3e

      Hi, I've written a windows service to parse files.It uses the FileSystemWatcher class in C# to sniff a folder for particular type of files. But when the files were being processed, i repeatedly get the error "The process cannot access the file "AAA" because it is being used by another process". To overcome this problem, i added the following. lines of code

      public bool ParseEnvFile(string strFilePath)
      {
      try
      {
      using(FileStream fs = new FileStream(strFilePath, FileMode.Open, FileAccess.Read, FileShare.Read))
      {
      // Do some processing here
      fs.Close();
      m_nReadAttempts = 0;
      }
      }
      catch(IOException ex)
      {
      if(m_nReadAttempts < 3)
      {
      Thread.Sleep(2000);
      m_nReadAttempts++;
      return ParseEnvFile(strFilePath);
      }
      else
      {
      throw ex;
      }
      }
      .....
      }

      But even this did not solve the problem. The exception still kept coming, though less frequently. So I was forced to add a Timer event(runs every 5 mins) that would pick up all the files that were left over because of the exception. Now, my problem is this: When a file gets created in the folder, sometimes both the OnFileCreated event and the timer event try to access the file at the same time. Within my service, I'm required to open the file in write mode and update some contents. I find that at times the file that gets created ends up as a 0KB file (Note: when it initially got created the file had some content in it)!!!! Any ideas on how to solve it? Thanks for any help in resolving this problem.

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      Hi! Perhaps you should check _which_ other program is accessing your files. I've had a similar problem and it turned out that a virus scanner was the culprid. One other thing: Do you really want to have recursion in your code? I'd say a do/while loop would be clearer, but that's just my opinion... Regards, mav

      G 1 Reply Last reply
      0
      • M mav northwind

        Hi! Perhaps you should check _which_ other program is accessing your files. I've had a similar problem and it turned out that a virus scanner was the culprid. One other thing: Do you really want to have recursion in your code? I'd say a do/while loop would be clearer, but that's just my opinion... Regards, mav

        G Offline
        G Offline
        g3e
        wrote on last edited by
        #3

        Hi mav.. Thanks for your reply. When i tried to debug the program when i first faced this problem I found that the FileSystemWatcher was only holding the file:confused: So when i tried google it, many others had also said the same. That is why the wait logic was introduced. Yes i can do away with the recursion logic. Regards, G3

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups