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. File System Watcher

File System Watcher

Scheduled Pinned Locked Moved C#
question
6 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.
  • K Offline
    K Offline
    kingletas
    wrote on last edited by
    #1

    Hey all, I currently developing an application where i need to "keep an eye" on certain files on any given folder... I am doing the following: System.IO.FileSystemWatcher watcher = new System.IO.FileSystemWatcher(); watcher.Path = @path; watcher.NotifyFilter = System.IO.NotifyFilters.CreationTime; watcher.IncludeSubdirectories = true; watcher.Filter = ".txt"; watcher.EnableRaisingEvents = true; watcher.Created+=new System.IO.FileSystemEventHandler(watcher_Created); watcher.Deleted +=new System.IO.FileSystemEventHandler(watcher_Deleted); // the events handlers watcher_Created(object sender, System.IO.FileSystemEventArgs e) { MessageBox.Show("File Created: "+ e.Name); System.IO.File.Delete(e.FullPath); } watcher_Deleted(object sender, System.IO.FileSystemEventArgs e) { MessageBox.Show("File Deleted: "+ e.Name); } However it seems that i am missing something here because I am creating a file in the given path but the events are not been triggered... What am i missing? Oh, i would also i like to know if a file is renamed how can i changed it back and if it is deleted how can i restore it? Thanks all!

    Luis E Tineo S

    A 1 Reply Last reply
    0
    • K kingletas

      Hey all, I currently developing an application where i need to "keep an eye" on certain files on any given folder... I am doing the following: System.IO.FileSystemWatcher watcher = new System.IO.FileSystemWatcher(); watcher.Path = @path; watcher.NotifyFilter = System.IO.NotifyFilters.CreationTime; watcher.IncludeSubdirectories = true; watcher.Filter = ".txt"; watcher.EnableRaisingEvents = true; watcher.Created+=new System.IO.FileSystemEventHandler(watcher_Created); watcher.Deleted +=new System.IO.FileSystemEventHandler(watcher_Deleted); // the events handlers watcher_Created(object sender, System.IO.FileSystemEventArgs e) { MessageBox.Show("File Created: "+ e.Name); System.IO.File.Delete(e.FullPath); } watcher_Deleted(object sender, System.IO.FileSystemEventArgs e) { MessageBox.Show("File Deleted: "+ e.Name); } However it seems that i am missing something here because I am creating a file in the given path but the events are not been triggered... What am i missing? Oh, i would also i like to know if a file is renamed how can i changed it back and if it is deleted how can i restore it? Thanks all!

      Luis E Tineo S

      A Offline
      A Offline
      Abhijit Jana
      wrote on last edited by
      #2

      kingletas wrote:

      watcher.NotifyFilter = System.IO.NotifyFilters.CreationTime;

      You are missing in this line. your are notifying only the Creation time, but not file name !!! :) Try this one mywatcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess ; This will work.. and let me know when done !!!! Good luck

      Best Regards ----------------- Abhijit Jana View My CodeProject Articles "Success is Journey it's not a destination"

      K 1 Reply Last reply
      0
      • A Abhijit Jana

        kingletas wrote:

        watcher.NotifyFilter = System.IO.NotifyFilters.CreationTime;

        You are missing in this line. your are notifying only the Creation time, but not file name !!! :) Try this one mywatcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess ; This will work.. and let me know when done !!!! Good luck

        Best Regards ----------------- Abhijit Jana View My CodeProject Articles "Success is Journey it's not a destination"

        K Offline
        K Offline
        kingletas
        wrote on last edited by
        #3

        Thanks for replying but it seems that i am doing something terribly wrong:( because it is not working yet and to make sure i modified the notifier like this: watcher.NotifyFilter = System.IO.NotifyFilters.CreationTime | System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.Attributes | System.IO.NotifyFilters.Size | System.IO.NotifyFilters.Security| System.IO.NotifyFilters.LastAccess | System.IO.NotifyFilters.LastWrite; but no luck yet, maybe i need to add something else? What do you think?

        Luis E Tineo S

        A K 2 Replies Last reply
        0
        • K kingletas

          Thanks for replying but it seems that i am doing something terribly wrong:( because it is not working yet and to make sure i modified the notifier like this: watcher.NotifyFilter = System.IO.NotifyFilters.CreationTime | System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.Attributes | System.IO.NotifyFilters.Size | System.IO.NotifyFilters.Security| System.IO.NotifyFilters.LastAccess | System.IO.NotifyFilters.LastWrite; but no luck yet, maybe i need to add something else? What do you think?

          Luis E Tineo S

          A Offline
          A Offline
          Abhijit Jana
          wrote on last edited by
          #4

          can you infrom me actually what error you are getting ??? check this one...... private void File_Watcher(string sFolderPath) { FileSystemWatcher mywatcher = new FileSystemWatcher(sFolderPath); mywatcher.Filter = ""; mywatcher.NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastAccess ; mywatcher.EnableRaisingEvents = true; mywatcher.IncludeSubdirectories = true; mywatcher.Created += new FileSystemEventHandler(mywatcher_created); mywatcher.Deleted += new FileSystemEventHandler(mywatcher_deleted); mywatcher.Changed += new FileSystemEventHandler(mywatcher_changed); mywatcher.Renamed += new RenamedEventHandler(mywatcher_renamed); }

          Best Regards ----------------- Abhijit Jana View My CodeProject Articles "Success is Journey it's not a destination"

          1 Reply Last reply
          0
          • K kingletas

            Thanks for replying but it seems that i am doing something terribly wrong:( because it is not working yet and to make sure i modified the notifier like this: watcher.NotifyFilter = System.IO.NotifyFilters.CreationTime | System.IO.NotifyFilters.FileName | System.IO.NotifyFilters.Attributes | System.IO.NotifyFilters.Size | System.IO.NotifyFilters.Security| System.IO.NotifyFilters.LastAccess | System.IO.NotifyFilters.LastWrite; but no luck yet, maybe i need to add something else? What do you think?

            Luis E Tineo S

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

            I found what i was doing wrong :-).... on my desperation to get it to work i added: Watcher.BeginInit() when the form loaded, but once i removed that line it worked well... now any tip on how to restore it the files to what they were before any changes were made... like if renamed restore the file and if delete it restore it as well... I think a better question would be how can i backup an entire folder or how to store these files into a database without causing perfomance issues? -- modified at 0:53 Friday 16th November, 2007

            Luis E Tineo S

            A 1 Reply Last reply
            0
            • K kingletas

              I found what i was doing wrong :-).... on my desperation to get it to work i added: Watcher.BeginInit() when the form loaded, but once i removed that line it worked well... now any tip on how to restore it the files to what they were before any changes were made... like if renamed restore the file and if delete it restore it as well... I think a better question would be how can i backup an entire folder or how to store these files into a database without causing perfomance issues? -- modified at 0:53 Friday 16th November, 2007

              Luis E Tineo S

              A Offline
              A Offline
              Abhijit Jana
              wrote on last edited by
              #6

              kingletas wrote:

              I think a better question would be how can i backup an entire folder or how to store these files into a database without causing perfomance issues?

              For , that you have to use Background Worker.

              Best Regards ----------------- Abhijit Jana View My CodeProject Articles "Success is Journey it's not a destination"

              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