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. problems with FileSystemWatcher

problems with FileSystemWatcher

Scheduled Pinned Locked Moved C#
helpquestion
5 Posts 3 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.
  • S Offline
    S Offline
    stephan_007
    wrote on last edited by
    #1

    hi everyone! i use FileSystemWatcher to watch a directory for incoming files. this works fine except if the file is written to the directory (e.g. copied by hand or the system) the very moment the program wants to access it via the FileSystemWatcher routines or events. so it is locked by the copy handler of windows. is there a chance to find out if the file is ready to be accessed (e.g. read), because otherwise a file.ioexception will be thrown. meaning, i would have to delay the access to this file, until it is ready for access. i had the idea of skipping the file until the next event takes place, but what if this causes the same problem, then this won't work. do you have any ideas or is there an event like "file accessible" or is there a chance to find out if no other handler "posesses" the file? thanks. stephan.

    G K 2 Replies Last reply
    0
    • S stephan_007

      hi everyone! i use FileSystemWatcher to watch a directory for incoming files. this works fine except if the file is written to the directory (e.g. copied by hand or the system) the very moment the program wants to access it via the FileSystemWatcher routines or events. so it is locked by the copy handler of windows. is there a chance to find out if the file is ready to be accessed (e.g. read), because otherwise a file.ioexception will be thrown. meaning, i would have to delay the access to this file, until it is ready for access. i had the idea of skipping the file until the next event takes place, but what if this causes the same problem, then this won't work. do you have any ideas or is there an event like "file accessible" or is there a chance to find out if no other handler "posesses" the file? thanks. stephan.

      G Offline
      G Offline
      Giorgi Dalakishvili
      wrote on last edited by
      #2

      This is a common problem. Did you search codeproject or google? These are some of the results: FileSystemWatcher - Created events fires too early[^] File in use[^]

      #region signature my articles #endregion

      S 1 Reply Last reply
      0
      • G Giorgi Dalakishvili

        This is a common problem. Did you search codeproject or google? These are some of the results: FileSystemWatcher - Created events fires too early[^] File in use[^]

        #region signature my articles #endregion

        S Offline
        S Offline
        stephan_007
        wrote on last edited by
        #3

        so as far as i already read it is not too easy to just realize this within some minutes. it might be a trial and error until you find an acceptable solution for your own problem, right? i will try myself by tomorrow. but if someone already has a short solution "on the fly" please add it to my posting. i guess this might help everyone a lot. (some solutions might also be found in the links in the last posting by Giorgi Dalakishvili). thanks. stephan.

        S 1 Reply Last reply
        0
        • S stephan_007

          so as far as i already read it is not too easy to just realize this within some minutes. it might be a trial and error until you find an acceptable solution for your own problem, right? i will try myself by tomorrow. but if someone already has a short solution "on the fly" please add it to my posting. i guess this might help everyone a lot. (some solutions might also be found in the links in the last posting by Giorgi Dalakishvili). thanks. stephan.

          S Offline
          S Offline
          stephan_007
          wrote on last edited by
          #4

          i just found a simple solution but i am not very happy with it. i coded a function private bool IsAccessible(string Path) which checks, if the file is accessible or not. if it is the access trying to open the file is closed again, it returns true and the other operations continue. if it is not accessible, then the io-exception is caught and it returns false. when this function is called it is combined with a sleep(1000), meaning it tries to access the file, if it is accessible then it continues without waiting, if it is not, it waits for 1 sec. well you could reduce the amount of time for waiting to a fair value depending of the estimated time until the file should be ready. but after 1 sec most files are copied and ready for access :)

          1 Reply Last reply
          0
          • S stephan_007

            hi everyone! i use FileSystemWatcher to watch a directory for incoming files. this works fine except if the file is written to the directory (e.g. copied by hand or the system) the very moment the program wants to access it via the FileSystemWatcher routines or events. so it is locked by the copy handler of windows. is there a chance to find out if the file is ready to be accessed (e.g. read), because otherwise a file.ioexception will be thrown. meaning, i would have to delay the access to this file, until it is ready for access. i had the idea of skipping the file until the next event takes place, but what if this causes the same problem, then this won't work. do you have any ideas or is there an event like "file accessible" or is there a chance to find out if no other handler "posesses" the file? thanks. stephan.

            K Offline
            K Offline
            KKrista
            wrote on last edited by
            #5

            Hi Stephan, Before accessing a file for your processing, try opening a file with a WRITE access on it. If the file open fails with denied WRITE access, then another thread (Windows) might be using it. U can use this logic for any case (Files being copied, New file being created by another thread, etc.) Hope this helps...:) Regards, Krista

            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