How to check if a zip file is not in use by another process before downloading
<p>I'm writting a windows service that collects a zip file from a Uri path on a server. The problem I am running into is the zip file is still being written to on the server when my service attempts to collect it. If I put a Thread.Sleep(15000) before I make the call to download the file, it works fine. That delay is unacceptable when processing video alarm. If possible I would prefer to add a check in while loop to see if the file is ready to download from the server URI before calling the download function. Is this possible?</p>

<p>I'm using an asynchronous WebClient to download the zip file:</p>

<pre><code>internal void DownloadZippedFile(string eventId)
{
    try
    {
            Logger.LogInformation("Extracing Video for event: " + eventId);

            using (WebClient webClient = new WebClient())
            {
                string urlPath = sureViewZipHost + "/archiveevent?user=" + sureViewUserId + "&amp;password=" + sureViewPassword + "&amp;event=" + eventId;
                string tempPath = Path.GetTempPath();

                long lReceived = 0;
                long lTotal = 0;

                webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(DownloadCompleted);
                webClient.DownloadProgressChanged += delegate(object sender, DownloadProgressChangedEventArgs e)
                {
                    Logger.LogInformation("downloaded {0} of {1} bytes. {2}% complete", e.BytesReceived, e.TotalBytesToReceive, e.ProgressPercentage);

                    // Assign to outer variables to allow busy-wait to check.
                    lReceived = e.BytesReceived;
                    lTotal = e.TotalBytesToReceive;
                };

                webClient.DownloadFileAsync(new Uri(urlPath), tempPath + "//" + eventId + ".zip",eventId);
            }

        }
        catch (Exception ex)
        {
            Logger.LogError("Error extracting Video " + ex.Message);
        }
    }
</code></pre>

<p>An exception never gets thrown using the code above. I found the issue by opening the partially downloaded zip, it contains:</p>

<p>400 BadRequest</p>

<blockquote>
  <p>System.IO.IOException: The process cannot access the file
  'C:\FileStore\G98\E16884\R6507679\JpegBackup022620132102384484.zip'
  because it is being used by another process.</p>
</blockquote>
