Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys

I am currently having some difficulty in reading the size of my file when using the
VB.NET
System.IO.FileSystemWatcher


I am able to pick up the file info when the file is being posted to the directory or sub directory issue is the file size is returning 0 i want to know how would i be able to get the file size to return the size of the file that would be stored on the disk?

What I have tried:

VB.NET
watchfolder = New System.IO.FileSystemWatcher

VB.NET
 watchfolder.Path = "C:\Test\"
        watchfolder.IncludeSubdirectories = True

         watchfolder.NotifyFilter = IO.NotifyFilters.DirectoryName
        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.FileName
        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.Attributes
        watchfolder.NotifyFilter = watchfolder.NotifyFilter Or IO.NotifyFilters.Size

        AddHandler watchfolder.Changed, AddressOf logchange
        AddHandler watchfolder.Created, AddressOf logchange
        AddHandler watchfolder.Deleted, AddressOf logchange

        AddHandler watchfolder.Renamed, AddressOf logrename

        watchfolder.EnableRaisingEvents = True

 Private Sub logchange(ByVal source As Object, ByVal e As  _
                       System.IO.FileSystemEventArgs)
        If e.ChangeType = IO.WatcherChangeTypes.Changed Then
  Dim FileSize As Long = New System.IO.FileInfo(e.FullPath).Length
End If
If e.ChangeType = IO.WatcherChangeTypes.Created Then
  Dim FileSize As Long = New System.IO.FileInfo(e.FullPath).Length
End If
If e.ChangeType = IO.WatcherChangeTypes.Deleted Then
  Dim FileSize As Long = New System.IO.FileInfo(e.FullPath).Length
End If
Posted
Comments
Paw Jershauge 19-Aug-16 6:39am    
Unless the program in question (the application thats writing the file) is pre-allocating the disc space, you wont be able to get that information. i guess
_Asif_ 19-Aug-16 7:19am    
This link suggest a workaround. https://social.msdn.microsoft.com/Forums/vstudio/en-US/7be543f0-b9d8-49e4-be4a-4c104ecfa139/length-property-of-fileinfo-class-returns-zero-size-when-the-file-coping?forum=netfxbcl

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900