Will_G
Will_G

Reputation: 269

Download full directory with subdirectories with FTP C#

How can I download a full directory from the ftpserver?

I can download with this 1 file:

try
        {                
            string strUri = strDsiteAdres+ "//" + file+"/";
            Uri serverUri = new Uri(strDsiteAdres);
            if (serverUri.Scheme != Uri.UriSchemeFtp)
            {
                return;
            }       
            FtpWebRequest reqFTP;
            reqFTP = (FtpWebRequest)FtpWebRequest.Create(strUri);                                
            reqFTP.Credentials = new NetworkCredential(strDusername, strDpassword);                
            reqFTP.KeepAlive = false;                
            reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;                                
            FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
            Stream responseStream = response.GetResponseStream();
            FileStream writeStream = new FileStream(strPath +"/"+ file, 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(); 
        }

Can someone please help me?

Upvotes: 1

Views: 3556

Answers (1)

Drew
Drew

Reputation: 24949

you can check out http://www.laedit.net/fr/Framework/code/Net-FTPClient.html

grab that little box in the lower right and stretch it to get the code. it has a missing function OnDownloadFilesOrCreateDirectory and a few other messes but clearly the code is there to recurse the directories if you want to roll up your sleeves with it

Upvotes: 1

Related Questions