Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

How to download data from http:\\ftp using c# .net windows service?

like my ftp is:- https://ftp.abc.com/[^]

Thanks in Advance.

Ankit Agarwal
Software Engineer
Posted

C#
/* Create Object Instance */
ftp ftpClient = new ftp(@"ftp://10.10.10.10/", "user", "password");

/* Download a File */
ftpClient.download("etc/test.txt", @"C:\Users\metastruct\Desktop\test.txt");

using
Simple C# FTP Class[^]
 
Share this answer
 
Below is the download function for ftp download

C#
public static void Download(string FTPFullFileName, string DestFullFileName, string UserName, string Password)
        {
            try
            {
                DestFullFileName = DestFullFileName.Replace("/", "\\");
                int indexPart = DestFullFileName.LastIndexOf("\\");
                if (indexPart != -1)
                {
                    String DestFolder = DestFullFileName.Substring(0, indexPart + 1);

                    if (!Directory.Exists(DestFolder))
                        Directory.CreateDirectory(DestFolder);
                }
                FtpWebRequest FTP = (FtpWebRequest)FtpWebRequest.Create(FTPFullFileName);
                FTP.Method = WebRequestMethods.Ftp.DownloadFile;
                FTP.UseBinary = true;
                FTP.Proxy = null;
                FTP.Credentials = new NetworkCredential(UserName, Password);
                FtpWebResponse Response = (FtpWebResponse)FTP.GetResponse();
                Stream FtpStream = Response.GetResponseStream();
                int BufferSize =Convert.ToInt32(FileSize(FTPFullFileName, UserName, Password)) ;
                byte[] Buffer = new byte[BufferSize];
                FileStream OutputStream = new FileStream(DestFullFileName, FileMode.Create);
                int ReadCount = FtpStream.Read(Buffer, 0, BufferSize);
                while (ReadCount > 0)
                {
                    OutputStream.Write(Buffer, 0, ReadCount);
                    ReadCount = FtpStream.Read(Buffer, 0, BufferSize);
                }
                FtpStream.Close();
                OutputStream.Close();
                Response.Close();
            }
            catch (Exception ex)
            {
               // throw new Exception(ex.Message);
            }
        }


Below is the function to calculate downloading file size

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 the download function like below

C#
Download(@"ftp:\\192.xxx.xx.xx\inetpub\ftproot\RetailAgreement\abc.JPG", @"E:\c.jpg", ftpUserID, ftpPassword);
 
Share this answer
 
See this[^] link.

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