csv-logoThe requirement of downloading all CSV or XLSX on FTP server in asp.net using c#. I have share the code how I have solve this issue. We can download any type of file from server using this code. Just change the Extension of the file or mention multiple files extension.

Code to Download .CSV or .XLSX Files from FTP Server:

protected void Page_Load(object sender, EventArgs e)
{
string FtpServer=”ftp://–.–.–.- /”;
string username=”username”;
string password=”password”;
string localpath=@”D:\\FolderName\”;
DownloadFile(FtpServer,username,password,localpath);

}

public void DownloadFile(string FtpServer,string username,string password,string localpath)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(FtpServer);
request.Credentials = new NetworkCredential(username,password );
request.Method = WebRequestMethods.Ftp.ListDirectory;
StreamReader streamReader = new StreamReader(request.GetResponse().GetResponseStream());
string fileName3 = streamReader.ReadLine();
List<string> directories = new List<string>();

while (fileName3 != null && fileName3 != “testing12”)
{
if (Path.GetExtension(fileName3) == “.csv”) //or .xlsx
{
directories.Add(fileName3);
}

fileName3 = streamReader.ReadLine();

}

streamReader.Close();
using (WebClient ftpClient = new WebClient())
{
ftpClient.Credentials = new System.Net.NetworkCredential(username , password );

for (int i = 0; i <= directories.Count – 1; i++)
{
if (directories[i].Contains(“.”))
{

string path = FtpServer + directories[i].ToString();
string trnsfrpth = localpath + directories[i].ToString();
ftpClient.DownloadFile(path, trnsfrpth);
}
}
}

}

 

Written by 

I am Sakthivel Madesh, a certified Salesforce Advanced Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 15x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead 5x Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply

Your email address will not be published. Required fields are marked *

*