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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Filtering FileSystemWatcher Events By File Size

Filtering FileSystemWatcher Events By File Size

Scheduled Pinned Locked Moved C#
helpquestion
12 Posts 3 Posters 2 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.
  • R Offline
    R Offline
    rich_wenger
    wrote on last edited by
    #1

    Hi, I am attempting to create a service to watch particular directories for excessively large files being created. I've created a Created event handler with the following code: void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e) { FileInfo fi = new FileInfo(e.FullPath); if (fi.Length > 524288000)// 500MB = 524288000 { smtpMessage.Body = "A file exceeding 500 MB has been created at " + e.FullPath.ToUpper(); Client.Send(smtpMessage); } } I drop files one at a time into the watched directory and I'll get an email alert for the first file but nothing for subsequent files? Any help greatly appreciated.

    C. Richard Wenger System Administrator

    realJSOPR D 2 Replies Last reply
    0
    • R rich_wenger

      Hi, I am attempting to create a service to watch particular directories for excessively large files being created. I've created a Created event handler with the following code: void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e) { FileInfo fi = new FileInfo(e.FullPath); if (fi.Length > 524288000)// 500MB = 524288000 { smtpMessage.Body = "A file exceeding 500 MB has been created at " + e.FullPath.ToUpper(); Client.Send(smtpMessage); } } I drop files one at a time into the watched directory and I'll get an email alert for the first file but nothing for subsequent files? Any help greatly appreciated.

      C. Richard Wenger System Administrator

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #2

      How are you initializing the watcher?

      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

      R 1 Reply Last reply
      0
      • R rich_wenger

        Hi, I am attempting to create a service to watch particular directories for excessively large files being created. I've created a Created event handler with the following code: void fileSystemWatcher1_Created(object sender, FileSystemEventArgs e) { FileInfo fi = new FileInfo(e.FullPath); if (fi.Length > 524288000)// 500MB = 524288000 { smtpMessage.Body = "A file exceeding 500 MB has been created at " + e.FullPath.ToUpper(); Client.Send(smtpMessage); } } I drop files one at a time into the watched directory and I'll get an email alert for the first file but nothing for subsequent files? Any help greatly appreciated.

        C. Richard Wenger System Administrator

        D Offline
        D Offline
        Dave Kreskowiak
        wrote on last edited by
        #3

        rich_wenger wrote:

        FileInfo fi = new FileInfo(e.FullPath); if (fi.Length > 524288000)// 500MB = 524288000

        This isn't going to work. When a file is created, it's length is always 0. Only when the application flushes it's writes to the file does the filesize increase, for which there is NO FSW event. Your code has to keep track of the files that are new, then occasionally check back with those files to get their current sizes, then you can do what's necessary.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        R 1 Reply Last reply
        0
        • realJSOPR realJSOP

          How are you initializing the watcher?

          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
          -----
          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

          R Offline
          R Offline
          rich_wenger
          wrote on last edited by
          #4

          This is what I have setup: this.fileSystemWatcher1.Path = directoryPath1; this.fileSystemWatcher1.IncludeSubdirectories = true; this.fileSystemWatcher1.Filter = "*.*"; this.fileSystemWatcher1.Created += new FileSystemEventHandler(fileSystemWatcher1_Created);

          C. Richard Wenger System Administrator

          realJSOPR 1 Reply Last reply
          0
          • D Dave Kreskowiak

            rich_wenger wrote:

            FileInfo fi = new FileInfo(e.FullPath); if (fi.Length > 524288000)// 500MB = 524288000

            This isn't going to work. When a file is created, it's length is always 0. Only when the application flushes it's writes to the file does the filesize increase, for which there is NO FSW event. Your code has to keep track of the files that are new, then occasionally check back with those files to get their current sizes, then you can do what's necessary.

            A guide to posting questions on CodeProject[^]
            Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                 2006, 2007, 2008

            R Offline
            R Offline
            rich_wenger
            wrote on last edited by
            #5

            I think I understand what you are saying, I am just not certain why I am getting the first email alert?

            C. Richard Wenger System Administrator

            D 1 Reply Last reply
            0
            • R rich_wenger

              This is what I have setup: this.fileSystemWatcher1.Path = directoryPath1; this.fileSystemWatcher1.IncludeSubdirectories = true; this.fileSystemWatcher1.Filter = "*.*"; this.fileSystemWatcher1.Created += new FileSystemEventHandler(fileSystemWatcher1_Created);

              C. Richard Wenger System Administrator

              realJSOPR Offline
              realJSOPR Offline
              realJSOP
              wrote on last edited by
              #6

              You haven't set a NotifyFilter, and you haven't turned on EnableRaisingEvents...

              "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
              -----
              "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

              R 1 Reply Last reply
              0
              • realJSOPR realJSOP

                You haven't set a NotifyFilter, and you haven't turned on EnableRaisingEvents...

                "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                -----
                "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                R Offline
                R Offline
                rich_wenger
                wrote on last edited by
                #7

                Sorry, I've got EnableRaisingEvents = true in the OnStart method for the service. I'm not certain how I would code a NotifyFilter since none of them seem to relate to file size?

                C. Richard Wenger System Administrator

                D realJSOPR 2 Replies Last reply
                0
                • R rich_wenger

                  I think I understand what you are saying, I am just not certain why I am getting the first email alert?

                  C. Richard Wenger System Administrator

                  D Offline
                  D Offline
                  Dave Kreskowiak
                  wrote on last edited by
                  #8

                  FSW events don't fire exactly when the actual event occurs. In your case, it appears that the system was doning something else, blocking your application, but still queueing up the Create event. By the time your Create handler got called, the file had attained the require size to trip the email.

                  A guide to posting questions on CodeProject[^]
                  Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                       2006, 2007, 2008

                  1 Reply Last reply
                  0
                  • R rich_wenger

                    Sorry, I've got EnableRaisingEvents = true in the OnStart method for the service. I'm not certain how I would code a NotifyFilter since none of them seem to relate to file size?

                    C. Richard Wenger System Administrator

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #9

                    There are no file size filters in the FSW. The filter is just there so the FSW knows which events you want to check for. The FSW polls the filesystem, taking snapshot after snapshot of the monitored directies and their files Created and LastModified times. Then it compares the new snapshot with the old one, generating the required events based on the differences between the two snapshots, subject to the NotifyFilter specification.

                    A guide to posting questions on CodeProject[^]
                    Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                         2006, 2007, 2008

                    1 Reply Last reply
                    0
                    • R rich_wenger

                      Sorry, I've got EnableRaisingEvents = true in the OnStart method for the service. I'm not certain how I would code a NotifyFilter since none of them seem to relate to file size?

                      C. Richard Wenger System Administrator

                      realJSOPR Offline
                      realJSOPR Offline
                      realJSOP
                      wrote on last edited by
                      #10

                      Well, I see a NotifyFilters.Size - isn't that what you want? At the same time, it might be better to filter on NotifyFilters.LastWrite so that you only have to deal with one event when the file is closed. At that point, you see how big the file is, and act accordingly, but you only do it once per file. Much more efficient.

                      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                      -----
                      "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                      R 1 Reply Last reply
                      0
                      • realJSOPR realJSOP

                        Well, I see a NotifyFilters.Size - isn't that what you want? At the same time, it might be better to filter on NotifyFilters.LastWrite so that you only have to deal with one event when the file is closed. At that point, you see how big the file is, and act accordingly, but you only do it once per file. Much more efficient.

                        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                        -----
                        "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                        R Offline
                        R Offline
                        rich_wenger
                        wrote on last edited by
                        #11

                        I looking over what you've said and I'm trying to see if there is a way I can leverage this to a useable outcome. As I read it the NotifyFilters.Size reports a CHANGE in file size and I'm not certain how I could use this in my application. The goal is to send email alerts only when files are copied into a directory over a predertimed size limit (if that helps).

                        C. Richard Wenger System Administrator

                        realJSOPR 1 Reply Last reply
                        0
                        • R rich_wenger

                          I looking over what you've said and I'm trying to see if there is a way I can leverage this to a useable outcome. As I read it the NotifyFilters.Size reports a CHANGE in file size and I'm not certain how I could use this in my application. The goal is to send email alerts only when files are copied into a directory over a predertimed size limit (if that helps).

                          C. Richard Wenger System Administrator

                          realJSOPR Offline
                          realJSOPR Offline
                          realJSOP
                          wrote on last edited by
                          #12

                          Then you want to set the LastWrite filter. That way, when the file is closed, you'll be notified, and you can check the size. If your goal is to determine the size of the file *before* it's written, I don't think that's possible.

                          "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
                          -----
                          "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

                          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