36 lines
1.0 KiB
C#
36 lines
1.0 KiB
C#
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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|