Solved

FtpWebRequest Not Working?

Posted on 2010-08-17
2
617 Views
Last Modified: 2012-05-10
Windows Service project: I'm using a foreach to download files from three different ftp directories. I don't receive any errors, ftp just stops working after trying to get the first file. I know the url is corrent and I have downloaded the files by hand. Now I'm trying to automate the file downloads. Please check-out this code and give me suggestions. Note: Also running 'Download' on a seperate thread did not make any difference.

private void Download(string ftpSource, string localDestination)
        {                      
            try
            {                
                string uri = "ftp://ftp.microsoft.com/Softlib" + ftpSource;
                                     
                FtpWebRequest ftp;                
                ftp = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));                    
                ftp.KeepAlive = false;                
                ftp.Method = WebRequestMethods.Ftp.DownloadFile;                                
                ftp.UseBinary = true;
                ftp.Proxy = null;                
                ftp.UsePassive = false;
                FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();

                Stream responseStream = response.GetResponseStream();
                FileStream writeStream = new FileStream(localDestination, FileMode.Create);                
                int Length = 2048;
                Byte[] buffer = new Byte[Length];
                int bytesRead = responseStream.Read(buffer, 0, Length);    
         
                while (bytesRead > 0)
                {
                    writeStream.Write(buffer, 0, bytesRead);
                    bytesRead = responseStream.Read(buffer, 0, Length);
                }              
 
                writeStream.Close();
                response.Close();
            }
            catch (WebException wex)
            {
                error("Download WebException: " + wex.ToString());
            }
            catch (Exception ex)
            {
                error("Download Exception: " + ex.ToString());
            }
        }
0
Comment
Question by:pointeman
2 Comments
 
LVL 5

Expert Comment

by:truromeo4juliet
Comment Utility
what about changing

while (bytesRead > 0)

to

while (bytesRead >= 0)
0
 

Accepted Solution

by:
pointeman earned 0 total points
Comment Utility
That didn't make any difference, but removing this made everything work. I have found this very code example on the Internet too.
  writeStream.Close();
                response.Close();
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now