/// <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.