Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
FileSystemWatcher
Message
General information
Forum:
ASP.NET
Category:
Other
Miscellaneous
Thread ID:
01102632
Message ID:
01102738
Views:
15
Sarosh,

I do it as Al described. Here is some of the actual code I've used for this:
        /// <summary>
        /// Determines if the file is ready for processing by trying to open it exclusively.
        /// </summary>
        /// <returns>True if the file can be opened exclusively. False if not.</returns>
        private bool isReady()
        {
            // Default the number of attemps.
            return this.isReady(Settings.Default.WaitAttempts);
        }

        /// <summary>
        /// Determines if the file is ready for processing by trying to open it exclusively.
        /// </summary>
        /// <param name="attempts">The number of attempts to make to open the file exclusively.</param>
        /// <returns>True if the file can be opened exclusively. False if not.</returns>
        private bool isReady(int attempts)
        {
            // Default the time to wait between attempts.
            return this.isReady(attempts, Settings.Default.WaitSeconds * 1000);
        }

        /// <summary>
        /// Determines if the file is ready for processing by trying to open it exclusively.
        /// </summary>
        /// <param name="attempts">The number of attempts to make to open the file exclusively.</param>
        /// <param name="wait">The number of milliseconds to wait between attempts.</param>
        /// <returns>True if the file can be opened exclusively. False if not.</returns>
        private bool isReady(int attempts, int wait)
        {
            bool success = false;

            // Default the number of attemps.
            if (attempts < 1)
            {
                attempts = Settings.Default.WaitAttempts;
            }

            // Default the time to wait between attempts.
            if (wait <= 0)
            {
                wait = Settings.Default.WaitSeconds * 1000;
            }

            // Loop for up to the number of attempts specified.
            for (int i = 0; i < attempts; i++)
            {
                try
                {
                    // Open the file exclusively with automatic, immediate disposal.
                    using (FileStream fs = File.Open(Settings.Default.ImportFolder + this.fileName, 
                        FileMode.Open, FileAccess.ReadWrite, FileShare.None))
                    {
                        // The file is now opened exclusively.
                        success = true;
                    }
                }
                catch
                {
                    // The file could not be opened exclusively.
                    success = false;
                }

                // Exit the loop if the file was opened exclusively.
                if (success)
                {
                    break;
                }

                // Wait for the specified amount of time before the next attempt.
                Thread.Sleep(wait);
            }

            // Return the successful opening status.
            return success;
        }
Obviously, only the third method is required. The first two overloads are just there for added flexibility. In the past, I had tried to monitor the file size and see when it stopped changing, but I found that to not be reliable as some files wouldn't report their size changes until the write operation was complete. I haven't had any problems with the method shown above. With this method, you need only check:
if (file.isReady())
{
    // Rename and copy
}
Depending on how you set your default values for attempts and wait time, you can get just the results you'd expect I would think.

HTH,
Chad



>Hi!
>
>I was wondering how people are using FileSystemWatcher since this issue is not handled natively.
>
>Sarosh
>
>>>Hi!
>>>
>>>I have to create an application (C# or VB.NET) which needs to watch a network folder and once a file is copied into that folder, it needs to rename that file and copy it into another folder.
>>>
>>>The problem with the FileSystemWatcher class is that we don't know when exactly a file copy is complete in order to process (rename/copy etc.) that file.
>>>
>>>e.g. A big 200 MB file is dragged and dropped into the watch folder the FileSystemWatcher will fire the Created/Changed event but at that point the OS has still not finished copying that file so I cannot really process it.
>>>
>>>FileSystemWatcher fires events like Changed, Created, Renamed, Deleted but nothing like CopyCompleted or something like that so how should I go about doing this?
>>
>>Just a general idea - maybe you could try to open the file with a low-level file function (i.e. exclusive use). If the first attempt fails, keep trying (with maybe a 100 millisecond wait/sleep between attempts) until the call succeeds.
_________________________________
There are 2 types of people in the world:
    Those who need closure
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform