Solved

FtpWebRequest Not Working?

Posted on 2010-08-17
2
621 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple Injector with Web Service 4 55
Cant save 3D 4 23
Get distinct values from excel or comm seperated file 4 32
C# Formatting DateTime String Value 5 35
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…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

820 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