1

In my application, I have a FTP server containing some folders and those folders contain number of files and some are empty folders. I need to download all the folders as well as files into my local folder. How can I download them to my local folder?

I used the code below and got an exception:

public void DownloadFtpDirectory(string url, NetworkCredential credentials, string localPath)
{
    FtpWebRequest listRequest = (FtpWebRequest)WebRequest.Create(url);
    listRequest.Method = WebRequestMethods.Ftp.ListDirectoryDetails;
    listRequest.Credentials = credentials;

    List<string> lines = new List<string>();

    using (FtpWebResponse listResponse = (FtpWebResponse)listRequest.GetResponse())
    using (Stream listStream = listResponse.GetResponseStream())
    using (StreamReader listReader = new StreamReader(listStream))
    {
        while (!listReader.EndOfStream)
        {
            lines.Add(listReader.ReadLine());
        }
    }

    foreach (string line in lines)
    {
        string[] tokens =
        line.Split(new[] { ' ' }, 9, StringSplitOptions.RemoveEmptyEntries);
        string name = tokens[8];
        string permissions = tokens[0];

        string localFilePath = Path.Combine(localPath, name);
        string fileUrl = url + name;

        if (permissions[0] == 'c')
        {
            if (!Directory.Exists(localFilePath))
            {
                Directory.CreateDirectory(localFilePath);
            }
            DownloadFtpDirectory(fileUrl + "/", credentials, localFilePath);
        }
        else
        {
            FtpWebRequest downloadRequest = (FtpWebRequest)WebRequest.Create(fileUrl);
            downloadRequest.Method = WebRequestMethods.Ftp.DownloadFile;
            downloadRequest.Credentials = credentials;

            using (FtpWebResponse downloadResponse = (FtpWebResponse)downloadRequest.GetResponse())
            using (Stream sourceStream = downloadResponse.GetResponseStream())
            using (Stream targetStream = File.Create(localFilePath))
            {
                byte[] buffer = new byte[10240];
                int read;
                while ((read = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    targetStream.Write(buffer, 0, read);
                }
            }
        }
    }
}

enter image description here Exception

  • You can change the permission of the folder. Have a look at [this answer](http://stackoverflow.com/a/24796126/5588347). – StackUseR Nov 28 '16 at 06:18
  • 1
    What is the `url` for which you get the exception? Can you download the same file using a regular FTP client (WinSCP, FileZilla)? Show us its log file. – Martin Prikryl Nov 28 '16 at 07:26
  • I don't have folders contains sub files and i have 18 folders some are empty and some are contains files. In the above code i made changes in `permissions[0] == 'd' to permissions[0] == 'c'` and i am getting above exception. when take `permissions[0] == 'd'` it will download empty folders and any folders contains files it will shows Exception in above i attached image name is `Exception` please see that image. –  Nov 28 '16 at 07:59
  • You obviously cannot "download" a directory. That explains the `WebException`. – Martin Prikryl Nov 28 '16 at 08:21
  • Regarding the `UnauhorizedAccessException`: What is your stating `url` and `localPath`? + What is the `url` and `localPath` at the moment you get the exception? – Martin Prikryl Nov 28 '16 at 08:22
  • I take `url="ftpserver url" and localpath="In my application I created one folder"`. If i don't have sub folders in my folders in that case also is it work fine. –  Nov 28 '16 at 08:59
  • I hope you can imaging that your last comment is totally useless. We have to know real values. Both the initial values for your root call of the `DownloadFtpDirectory` and the values in the call that throws. – Martin Prikryl Nov 28 '16 at 09:49
  • Hi Martin Prikryl, I am taking like this `NetworkCredential credentials = new NetworkCredential("username", "password"); string url = "ftp://ftp.eli.com/xxx/Fromxxx/foldername/"; string localDestnDir = @"C:\Users\xxx\Desktop\New folder (4)\DownloadfilesfromFTP\DownloadfilesfromFTP\DownloadFTP"; DownloadFtpDirectory(url, credentials, localDestnDir);` can you please tell me. thank you –  Nov 28 '16 at 10:00
  • Once again: What are the values of `url` and `localPath`, at the moment the exception is thrown? – Martin Prikryl Nov 28 '16 at 12:35
  • `localpath = C:\Users\Name\Desktop\New folder (4)\DownloadfilesfromFTP\DownloadfilesfromFTP\DownloadFTP\FolderName1\FilenameO210062016152424.doc. url = ftp://ftp.xxx.com/elcs/Fromelcs/Main/FolderName1/FilenameO210062016152424.doc` –  Nov 28 '16 at 12:56

0 Answers0