Solved

FtpWebRequest Not Working?

Posted on 2010-08-17
2
619 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
ID: 33458604
what about changing

while (bytesRead > 0)

to

while (bytesRead >= 0)
0
 

Accepted Solution

by:
pointeman earned 0 total points
ID: 33459000
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

914 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

21 Experts available now in Live!

Get 1:1 Help Now