Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Read Multiple Files From Folder And Download Using FTP location In Console Application Using C#
WhatsApp
Ananth G
8y
26.3
k
0
1
25
Blog
Introduction
Here, I will explain how to read multiple files from the folder, using FTP location. Please follow the steps mentioned below to fetch.
Follow the steps mentioned below to achieve our requirement.
Step 1
Place the code in Class file
string
ftpDownloadResult =
string
.Empty;
try
{
WebRequest request = WebRequest.Create(ConfigurationManager.AppSettings[
"FTPPath"
]);
request.Method = WebRequestMethods.Ftp.ListDirectory;
request.Credentials = newNetworkCredential(ConfigurationManager.AppSettings[
"FTPUserName"
],
ConfigurationManager.AppSettings[
"FTPPassword"
]);
using
(var response = (FtpWebResponse) request.GetResponse())
{
StreamReader streamReader = newStreamReader(response.GetResponseStream());
List <
string
> directories = newList <
string
> ();
string
line = streamReader.ReadLine();
while
(!
string
.IsNullOrEmpty(line))
{
directories.Add(line);
line = streamReader.ReadLine();
}
streamReader.Close();
using
(WebClient ftpClient = newWebClient())
{
ftpClient.Credentials =
new
System.Net.NetworkCredential(
ConfigurationManager.AppSettings[
"FTPUserName"
],
ConfigurationManager.AppSettings[
"FTPPassword"
]);
for
(
int
i = 0; i <= directories.Count– 1; i++)
{
if
(directories[i].Contains(“.”))
{
string
path = ConfigurationManager.AppSettings[
"FTPPath"
] + directories[i].ToString();
string
trnsfrpth = ConfigurationManager.AppSettings[
"SaveFilePath"
] + directories[i].ToString();
ftpClient.DownloadFile(path, trnsfrpth);
}
}
}
}
Step 2
Add the code, mentioned below to app.config
<
appSettings
>
<
add
key
=
"FTPPath"
value
=
"ftp://test.com/MyFolder"
>
</
add
>
<
add
key
=
"FTPUserName"
value
=
"USERNAME"
/>
<
add
key
=
"FTPPassword"
value
=
"PASSWORD"
/>
<
addkey
addkey
="SaveFilePath"
value
="C:\HRData\"
/>
<
/appSettings
>
<
system.net
>
<
defaultProxy
useDefaultCredentials
=
"true"
>
<
proxy
autoDetect
=
"True"
/>
<
/defaultProxy
>
<
/system.net
>
FTP location
Console Application
C#
Up Next
How To Get Application Startup Path From The Console Application Using C#
Ebook Download
View all
Pattern Matching in C#
Read by 1.5k people
Download Now!
Learn
View all
Membership not found