Read File from FTP location in Console Application using C#

Category: C#.Net Comments: No comments

Today I’m going to tell you to read text from FTP Location.Please follow below steps to fetch

Step 1 :-  Place code in Class file

WebRequest request = WebRequest.Create(ConfigurationManager.AppSettings["FTPPath"]);
request.Method = WebRequestMethods.Ftp.DownloadFile;
request.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["FTPUserName"],
ConfigurationManager.AppSettings["FTPPassword"]);
using (var responde = (FtpWebResponse)request.GetResponse())
{
Stream responseStream = responde.GetResponseStream();
StreamReader reader = new StreamReader(responseStream);
ftpDownloadResult = reader.ReadToEnd().Trim();
reader.Close();
reader.Dispose();
responde.Close();
}

Step 2:- Add credential in config Section

<appSettings>
<add key=”FTPPath” value=”ftp://test.com/key.txt”></add>
<add key=”FTPUserName” value=”USERNAME” />
<add key=”FTPPassword” value=”PASSWORD” />
</appSettings>

<system.net>
<defaultProxy useDefaultCredentials=”true”>
<proxy autoDetect=”True”/>
</defaultProxy>
</system.net>

 

Leave a Reply

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

*

HTML tags are not allowed.