コード例 #1
0
        private FtpWebRequest GetFtpWebRequest(string ftpPath, FTPSettings ftpSetting)
        {
            var listRequest = (FtpWebRequest)WebRequest.Create(ftpPath);

            listRequest.Credentials = new NetworkCredential(ftpSetting.Server.UserId, ftpSetting.Server.Password);
            return(listRequest);
        }
コード例 #2
0
        public async Task <List <DownLoadedFileModel> > DownLoadAsync(FTPSettings ftpSetting)
        {
            var SemaphoreForRecord           = new SemaphoreSlim(ftpSetting.SemaphoreSlimInit, ftpSetting.SemaphoreSlimMax);
            List <DownLoadedFileModel> files = new List <DownLoadedFileModel>();

            List <SftpFile> ftpFiles;
            List <Task <DownLoadedFileModel> > DLFileTasks = new List <Task <DownLoadedFileModel> >();
            var connInfoSource = new PasswordConnectionInfo(ftpSetting.Server.Host, ftpSetting.Server.Port, ftpSetting.Server.UserId, ftpSetting.Server.Password);

            using (var sftp = new SftpClient(connInfoSource))
            {
                sftp.Connect();
                ftpFiles = sftp.ListDirectory(ftpSetting.RootPath).Where(x => x.IsRegularFile).ToList();

                foreach (var file in ftpFiles)
                {
                    DLFileTasks.Add(DownLoadAFileProcessAsync(file, sftp, ftpSetting.OutPutPath, SemaphoreForRecord));
                }
                var downloadfiles = await Task.WhenAll(DLFileTasks);

                files = downloadfiles.Where(x => !string.IsNullOrEmpty(x.LocalPath)).ToList();
                sftp.Disconnect();
            }
            return(files);
        }
コード例 #3
0
        public async Task <List <DownLoadedFileModel> > DownLoadAsync(FTPSettings ftpSetting)
        {
            List <DownLoadedFileModel> result = null;
            var files = await GetRootFilesAsync(ftpSetting);

            if (files != null)
            {
                if (!Directory.Exists(ftpSetting.OutPutPath))
                {
                    Directory.CreateDirectory(ftpSetting.OutPutPath);
                }
                List <Task <DownLoadedFileModel> > tasks = new List <Task <DownLoadedFileModel> >();
                SemaphoreSlim SemaphoreForRecord         = new SemaphoreSlim(ftpSetting.SemaphoreSlimInit, ftpSetting.SemaphoreSlimMax);
                foreach (var item in files)
                {
                    tasks.Add(
                        DownLoadAFileProcessAsync(item, ftpSetting, SemaphoreForRecord)
                        );
                }
                var dlFiles = await Task.WhenAll(tasks);

                result = dlFiles.ToList();
            }
            return(result);
        }
コード例 #4
0
 public async Task DeleteFilesAsync(List <DownLoadedFileModel> files, FTPSettings ftpSetting)
 {
     if (files != null)
     {
         List <Task>   tasks = new List <Task>();
         SemaphoreSlim SemaphoreForRecord = new SemaphoreSlim(ftpSetting.SemaphoreSlimInit, ftpSetting.SemaphoreSlimMax);
         foreach (var item in files)
         {
             tasks.Add(
                 DeleteFtpFileAsync(item.FtpPath, ftpSetting, SemaphoreForRecord)
                 );
         }
         await Task.WhenAll(tasks.ToArray());
     }
 }
コード例 #5
0
        private async Task <List <string> > GetRootFilesAsync(FTPSettings ftpSetting)
        {
            string RootPath    = $"{ftpSetting.Server.Host}:{ftpSetting.Server.Port}/{ftpSetting.RootPath}";
            var    listRequest = GetFtpWebRequest(RootPath, ftpSetting);

            listRequest.Method = WebRequestMethods.Ftp.ListDirectory;
            List <string> files = new List <string>();

            using (var listResponse = listRequest.GetResponse())
            {
                using var listStream = listResponse.GetResponseStream();
                using var listReader = new StreamReader(listStream);
                while (!listReader.EndOfStream)
                {
                    files.Add($"{RootPath}/{await listReader.ReadLineAsync()}");
                }
            }
            return(files);
        }
コード例 #6
0
 private async Task DeleteFtpFileAsync(string file, FTPSettings ftpSetting, SemaphoreSlim SemaphoreForRecord)
 {
     await Task.Run(() =>
     {
         SemaphoreForRecord.Wait();
         try
         {
             var request    = GetFtpWebRequest(file, ftpSetting);
             request.Method = WebRequestMethods.Ftp.DeleteFile;
             using (var response = request.GetResponse())
             {
                 response.Close();
             }
         }
         catch (Exception ex)
         {
         }
         finally
         {
             SemaphoreForRecord.Release();
         }
     });
 }
コード例 #7
0
        private async Task <DownLoadedFileModel> DownLoadAFileProcessAsync(string file, FTPSettings ftpSetting, SemaphoreSlim SemaphoreForRecord)
        {
            /// if an exception occurred, the LocalPath is null and ExceptionMessage has value in DownLoadFileModel.
            return(await Task.Run(() =>
            {
                DownLoadedFileModel result = new DownLoadedFileModel();
                result.FtpPath = file;
                result.LocalPath = $"{ftpSetting.OutPutPath}\\{Path.GetFileName(file)}";

                SemaphoreForRecord.Wait();
                try
                {
                    var fileRequest = GetFtpWebRequest(file, ftpSetting);
                    fileRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                    using var response = fileRequest.GetResponse();
                    using var responseStream = response.GetResponseStream();
                    using var localFile = new FileStream(result.LocalPath, FileMode.Create, FileAccess.Write);
                    responseStream.CopyTo(localFile);
                    localFile.Flush();
                }
                catch (Exception ex)
                {
                    result.LocalPath = null;
                    result.ExceptionMessage = ex.Message;
                }
                finally
                {
                    SemaphoreForRecord.Release();
                }
                return result;
            }));
        }