Click here to Skip to main content
15,887,355 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I'm using a simple program that identifies changes in files in a certain folder specified by the user.
When I download a file through FTP, 2 files are created (one with final name (ex: .iso) and other .part) and two new file events are triggered. Is there any method to know when the downloaded file is completed?

Regards,
Pedro
Posted
Comments
Sergey Alexandrovich Kryukov 4-Mar-14 14:24pm    
It depends on how you download it. If you do it synchronously, the operation just blocks the calling thread until the operation is complete/failed. I would recommend using it this way, but in a separate thread (unless this is a simple console application).
If you have further concerns, show some code sample.
—SA

See the very last line of the code i.e Console.WriteLine().
C#
using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main ()
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            // This example assumes the FTP site uses anonymous logon.
            request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
    
            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            Console.WriteLine(reader.ReadToEnd());

            Console.WriteLine("Download Complete, status {0}", response.StatusDescription);
    
            reader.Close();
            response.Close();  
        }
    }
}

From MSDN[^]

-KR
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Mar-14 16:39pm    
It does illustrate right approach, a 5.
I already noted that in many cases the FTP communications should be done in a separate thread.
—SA
If you're talking about an external FTP app that you're using, seperate from your own code, no there is no way to determine when that file download is complete.

You can, however, just try to open the file, denying shared access to other applications. When you can finally open the file, the FTP download is done. Until then, the download is still going on.
 
Share this answer
 
Comments
Maciej Los 4-Mar-14 16:35pm    
Valuable hint, +5!
you can verify ur file completely downloaded by checking downloaded file size with ftp file size


for getting ftp file size use below function
C#
public static long FileSize(string FTPFullFileName, string UserName, string Password)
       {
           try
           {

               FtpWebRequest FTP = (FtpWebRequest)FtpWebRequest.Create(FTPFullFileName);
               FTP.Method = WebRequestMethods.Ftp.GetFileSize;
               FTP.UseBinary = true;
               FTP.Credentials = new NetworkCredential(UserName, Password);
               FtpWebResponse Response = (FtpWebResponse)FTP.GetResponse();
               Stream FtpStream = Response.GetResponseStream();
               long FileSize = Response.ContentLength;

               FtpStream.Close();
               Response.Close();
               return FileSize;
           }
           catch (Exception ex)
           {
               throw new Exception(ex.Message);
           }
       }


calling above function

C#
string ftpFileSize =Convert.ToString(FileSize(FTPFullFileName, UserName, Password)) ;



fetching downloaded file size using fileinfo

C#
string localFileSize=string.Empty;
  FileInfo info = new FileInfo("downloaded file path+filename");
    if (info.Exists)
     {
             localFileSize = info.Length.ToString();
     }


if localFileSize equals ftpFileSize ,ur download is complete

u have to make sure localFileSize & ftpFileSize are in same units else convert them to same unit

gud luck ;-)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900