When is file ready for using
When is file ready for using
When is file ready for using
When is file ready for using
When is file ready for using
When is file ready for using When is file ready for using When is file ready for using When is file ready for using When is file ready for using When is file ready for using When is file ready for using When is file ready for using
When is file ready for using When is file ready for using
When is file ready for using
Go Back  Xtreme Visual Basic Talk > > > When is file ready for using


Reply
 
Thread Tools Display Modes
  #1  
Old 12-06-2010, 12:46 PM
michelleqw michelleqw is offline
Junior Contributor
 
Join Date: Jun 2005
Posts: 212
Default When is file ready for using


Dear all,

Our program will start with the help of a FileSystemWatcher object.

How can we check or the file is total copied, for example the file is 100 Mbyte it take a time it's ready for use.

Idea:

Do While True
Try
N = testForUse(BigFile)
If N = 1 Then Exit Do
Catch ex As Exception
End Try
Loop

Nice regards,
Michelle.
Reply With Quote
  #2  
Old 12-06-2010, 03:36 PM
Mkjo99 Mkjo99 is offline
Regular
 
Join Date: May 2009
Posts: 53
Default

I work at a company where we process large numbers of faxed and scanned images on a daily basis. File transfers of large multi-page tiffs into our queue can take up to 45 minutes. I have not yet found a 100% fool-proof way to test a file to tell you that it has been created, closed and ready to work with.

The filesystemwatcher events will not tell you when the file is done being created (which you may have already found out). I used the filesystemwatcher to get the file name as it was being faxed or ftp'd to a queue. I then used a timer to periodically check the file to see if it was writeable:
Code:
Dim fs as FileStream
Try
  fs = New FileStream(sFileName, FileMode.OpenOrCreate, FileAccess.Write,    FileShare.None)
  If fs.CanWrite Then
     ' DoSomething()
  End If
Catch ex as Exception
     
End Try
Even this method is not perfect. I know of several instances where "fs.CanWrite" returned true before the file had finished being written to.
Reply With Quote
  #3  
Old 12-09-2010, 02:21 PM
michelleqw michelleqw is offline
Junior Contributor
 
Join Date: Jun 2005
Posts: 212
Default

Hello Mkjo99,

Thanks for the information.

Michelle.
Reply With Quote
  #4  
Old 12-10-2010, 01:36 AM
Goggy's Avatar
Goggy Goggy is offline
Contributor
 
Join Date: Sep 2005
Location: Enschede,The Netherlands
Posts: 670
Default

To expand on mkj99

maybe you could try to open it exclusively

Code:
 ''' <summary>
        ''' Check's if a file is in use or not.
        ''' <example><code>
        ''' If Not Miscellaneous.FileHelper.IsInUse("C:\Config.bat") Then
        '''     'Do something now the file isn't in use.
        ''' End if
        ''' </code></example>
        ''' </summary>
        ''' <param name="FileName"></param>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Shared Function IsInUse(ByVal FileName As String) As Boolean
            If System.IO.File.Exists(FileName) Then
                Try
                    'CREATE A FILE STREAM FROM THE FILE, OPENING IT FOR READ ONLY EXCLUSIVE ACCESS
                    Dim FS As System.IO.FileStream = System.IO.File.Open(FileName, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.None)
                    'CLOSE AND CLEAN UP RIGHT AWAY, IF THE OPEN SUCCEEDED, WE HAVE OUR ANSWER ALREADY
                    FS.Close()
                    FS.Dispose()
                    FS = Nothing
                    Return False
                Catch ex As IO.IOException
                    'IF AN IO EXCEPTION IS THROWN, WE COULD NOT GET EXCLUSIVE ACCESS TO THE FILE
                    Return True
                Catch ex As Exception
                    Return True
                End Try
            Else
                Return True
            End If
        End Function
__________________
As finishing touch, god created the Dutch!

utterly useless, but always trying to help
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
When is file ready for using
When is file ready for using
When is file ready for using When is file ready for using
When is file ready for using
When is file ready for using
When is file ready for using When is file ready for using When is file ready for using When is file ready for using When is file ready for using When is file ready for using When is file ready for using
When is file ready for using
When is file ready for using
 
When is file ready for using
When is file ready for using
 
-->