Link to home
Start Free TrialLog in
Avatar of Varshini S
Varshini S

asked on

FTP file download using c#

I am using the following method for download file from FTP.

public static string Download(string FTPServer, string remotePath, string fileNameToDownload, string saveToLocalPath, string user, string password,string ftpMode)
        {

            try
            {

                //Get FTP web resquest object.

                FtpWebRequest request = FtpWebRequest.Create(new Uri(FTPServer +  remotePath + "/"+fileNameToDownload)) as FtpWebRequest;

                request.UseBinary = true;

                request.KeepAlive = false;

                // if any firewall issue in ftp use passive mode 
                if (ftpMode.ToUpper() == "TRUE")
                {
                    request.UsePassive = true;
                }

                
                request.Method = WebRequestMethods.Ftp.DownloadFile;

                if (!string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(password))

                    request.Credentials = new NetworkCredential(user, password);



                FtpWebResponse response = request.GetResponse() as FtpWebResponse;

                Stream responseStream = response.GetResponseStream();

                FileStream outputStream = new FileStream(saveToLocalPath + "\\" + fileNameToDownload, FileMode.Create);



                int bufferSize = 1024;

                int readCount;

                byte[] buffer = new byte[bufferSize];



                readCount = responseStream.Read(buffer, 0, bufferSize);

                while (readCount > 0)
                {

                    outputStream.Write(buffer, 0, readCount);

                    readCount = responseStream.Read(buffer, 0, bufferSize);

                }

                string statusDescription = response.StatusDescription;

                responseStream.Close();

                outputStream.Close();

                response.Close();

                return statusDescription;

            }

            catch (Exception e)
            {

                //throw new Exception("Error downloading from URL " + "ftp://" + FTPServer + @"/" + remotePath + @"/" + fileNameToDownload, e);
                throw e;

            }

        }

Open in new window


This method works fine whenever using FTP credentials (FTP username and password).
But current scenario FTP in the IIS does not require username and password. Since it is inside the network it does not require any username and password. I have verified this using browser.  How can i use the same method without using username and password?

i am using vs2013.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
use the credential 'anonymous' password username@example.com  // an email address
Avatar of Varshini S
Varshini S

ASKER

I tried anonymous' password username@example.com   but it did not work.
remove that line worked for me.