Initial commit
This commit is contained in:
35
PartSource.Automation/Services/FtpService.cs
Normal file
35
PartSource.Automation/Services/FtpService.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using PartSource.Automation.Models.Configuration;
|
||||
using System;
|
||||
using System.Configuration;
|
||||
using System.IO;
|
||||
using System.Net;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace PartSource.Automation.Services
|
||||
{
|
||||
public class FtpService
|
||||
{
|
||||
private readonly FtpConfiguration _ftpConfiguration;
|
||||
|
||||
public FtpService(FtpConfiguration ftpConfiguration)
|
||||
{
|
||||
_ftpConfiguration = ftpConfiguration;
|
||||
}
|
||||
|
||||
public void Download(string filename)
|
||||
{
|
||||
FtpWebRequest request = (FtpWebRequest)WebRequest.Create($"{_ftpConfiguration.Url}/{filename}");
|
||||
request.Credentials = new NetworkCredential(_ftpConfiguration.Username, _ftpConfiguration.Password);
|
||||
request.Method = WebRequestMethods.Ftp.DownloadFile;
|
||||
|
||||
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
|
||||
{
|
||||
using (Stream responseStream = response.GetResponseStream())
|
||||
using (FileStream fileStream = new FileStream($"{_ftpConfiguration.Destination}\\{filename}", FileMode.Create))
|
||||
{
|
||||
responseStream.CopyTo(fileStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user