Monitoring files - how to know when a file is complete

前端 未结 9 1988
无人及你
无人及你 2021-01-11 12:59

We have several .NET applications that monitor a directory for new files, using FileSystemWatcher. The files are copied from another location, uploaded via FTP, etc. When th

相关标签:
9条回答
  • 2021-01-11 13:24

    The way I check in Windows if a file has been completely uploaded by ftp is to try to rename it. If renaming fails, the file isn't complete. Not very elegant, I admit, but it works.

    0 讨论(0)
  • 2021-01-11 13:26

    The "Changed" event on the FileSystemWatcher should shouldn't fire until the file is closed. See my answer to a similar question. There is a possibility that the FTP download mechanism closes the file multiple times during download as new data comes in, but I would think that is a little unlikely.

    0 讨论(0)
  • 2021-01-11 13:27

    You probably have to go with some out of band signaling: have the producer of "file.ext" write a dummy "file.ext.end".

    0 讨论(0)
  • 2021-01-11 13:28

    Have you tried getting a write lock on the file? If it's being written to, that should fail, and you know to leave it alone for a bit...

    0 讨论(0)
  • 2021-01-11 13:36

    A write lock doesn't help if the file upload failed part way through and the sender hasn't tried resending (and relocking) the file yet.

    0 讨论(0)
  • 2021-01-11 13:39

    The following method tries to open a file with write permissions. It will block execution until a file is completely written to disk:

    /// <summary>
    /// Waits until a file can be opened with write permission
    /// </summary>
    public static void WaitReady(string fileName)
    {
        while (true)
        {
            try
            {
                using (System.IO.Stream stream = System.IO.File.Open(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    if (stream != null)
                    {
                        System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} ready.", fileName));
                        break;
                    }
                }
            }
            catch (FileNotFoundException ex)
            {
                System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message));
            }
            catch (IOException ex)
            {
                System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message));
            }
            catch (UnauthorizedAccessException ex)
            {
                System.Diagnostics.Trace.WriteLine(string.Format("Output file {0} not yet ready ({1})", fileName, ex.Message));
            }
            Thread.Sleep(500);
        }
    }
    

    (from my answer to a related question)

    0 讨论(0)
提交回复
热议问题