Search code examples
ftpwebclientcredentialstransfer

How to download files from ftp to local directory using C#?


hi i am using the following code but it giving an error

 using (WebClient ftpClient = new WebClient())
        {
            ftpClient.Credentials = new System.Net.NetworkCredential("username", "password");
            ftpClient.DownloadFile("ftp://path.com/Business Plan.docx", "D:\\Folder\test.docx");
        }

but i am getting an error illegal characters in path.

I am not understanding how to do that.


Solution

  • This string:

    "D:\\Folder\test.docx"
    

    Is treating the slashes ('\') as escape characters - use this instead:

    @"D:\Folder\test.docx"
    

    Or (more messy), double escape to be treated as a literal slash:

    "D:\\Folder\\test.docx"