Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Determining last update of a file when it is in construc
Message
From
25/03/2014 16:24:30
 
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01597298
Message ID:
01597320
Views:
29
>I am writing something very similar right now, I am ftping 75Mb files and when they are ready I need to parse them, so when I detect a new file with the FileSystemWatcher class I wait until the file is ready by trying to open it exclusively, this is my code:
>
>
>        protected void WaitUntilFileIsReady(System.String fileName)
>        {
>            if (_eventLogger != null)
>            {
>                _eventLogger.LogEvent(string.Format("Waiting for file availability ({0} current size: {1} bytes", fileName, GetFileSize(fileName).ToString(System.Globalization.CultureInfo.InvariantCulture)), EventTypes.Wait);
>                while (!IsFileReady(fileName))
>                    System.Threading.Thread.Sleep(50);
>                _eventLogger.LogEvent(string.Format("The file {0} is available! ({1}) bytes", fileName, GetFileSize(fileName).ToString(System.Globalization.CultureInfo.InvariantCulture)), EventTypes.Available);
>            }
>        }
>
>        protected static bool IsFileReady(System.String fileName)
>        {
>            // If the file can be opened for exclusive access it means that the file
>            // is no longer locked by another process.
>            try
>            {
>                using (System.IO.FileStream inputStream = System.IO.File.Open(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None))
>                {
>                    return (inputStream.Length >= 0); 
>                }
>            }
>            catch (System.Exception)
>            {
>                return false;
>            }
>        }
>
>
>You can ignore the _eventLogger, as that is something I use to write into Windows Event Logger (and a SQL DB) to track activity.

Yes, this is something better. I can try to open it exclusively as you do and, if it succeeds, than I would proceed.

Thanks
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Previous
Reply
Map
View

Click here to load this message in the networking platform