Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

FtpWebRequest Not Working?

Posted on 2010-08-17
2
Medium Priority
?
625 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

650 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