Пример #1
0
        public bool DeleteFilesViaFTP(string fiPrefix)
        {
            try
            {
                // Delete exist files
                Logger.Info(_logMsg.Clear().SetPrefixMsg("Delete Files Via FTP").ToInputLogString());

                bool   isFileFound;
                string host            = WebConfig.GetBDWSshServer();
                int    port            = WebConfig.GetBDWSshPort();
                string username        = WebConfig.GetBDWSshUsername();
                string password        = WebConfig.GetBDWSshPassword();
                string remoteDirectory = WebConfig.GetBDWSshRemoteDir(); // . always refers to the current directory.

                using (var sftp = new SftpClient(host, port, username, password))
                {
                    sftp.Connect();
                    //var files = sftp.ListDirectory(remoteDirectory).Where(x => x.Name.ToUpperInvariant().Contains(fiPrefix.ToUpperInvariant()));
                    //isFileFound = files.Any();
                    isFileFound = _downloadList.Count > 0;

                    if (isFileFound)
                    {
                        // Download file to local via SFTP
                        foreach (var file in _downloadList)
                        {
                            DeleteFile(sftp, file.FullName);
                        }

                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Delete Files Via FTP").ToSuccessLogString());
                    }
                    else
                    {
                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Delete Files Via FTP").Add("Error Message", "File Not Found").ToFailLogString());
                    }

                    sftp.Disconnect();
                }
                return(isFileFound);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occur:\n", ex);
                Logger.Info(_logMsg.Clear().SetPrefixMsg("Delete Files Via FTP").Add("Error Message", ex.Message).ToInputLogString());
            }

            return(false);
        }
Пример #2
0
        /// <summary>
        /// This will list the contents of the current directory.
        /// </summary>
        public bool DownloadFilesViaFTP(string localPath, string fiPrefix)
        {
            try
            {
                // Delete exist files
                IEnumerable <string> localFiles = Directory.EnumerateFiles(localPath, string.Format(CultureInfo.InvariantCulture, "{0}*.*", fiPrefix)); // lazy file system lookup

                foreach (var localFile in localFiles)
                {
                    if (StreamDataHelpers.TryToDelete(localFile))
                    {
                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Delete exist local file").Add("FileName", localFile).ToSuccessLogString());
                    }
                    else
                    {
                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Delete exist local file").Add("FileName", localFile).ToFailLogString());
                    }
                }

                Logger.Info(_logMsg.Clear().SetPrefixMsg("Download Files Via FTP").Add("LocalPath", localPath).ToInputLogString());

                bool   isFileFound;
                string host            = WebConfig.GetBDWSshServer();
                int    port            = WebConfig.GetBDWSshPort();
                string username        = WebConfig.GetBDWSshUsername();
                string password        = WebConfig.GetBDWSshPassword();
                string remoteDirectory = WebConfig.GetBDWSshRemoteDir(); // . always refers to the current directory.

                using (var sftp = new SftpClient(host, port, username, password))
                {
                    sftp.Connect();
                    var files = sftp.ListDirectory(remoteDirectory).Where(x => x.Name.ToUpperInvariant().Contains(fiPrefix.ToUpperInvariant()));
                    var txt   = files.Where(x => Path.GetExtension(x.Name).ToUpperInvariant() == ".TXT");
                    var end   = files.Where(x => Path.GetExtension(x.Name).ToUpperInvariant() == ".END");

                    _downloadList.Clear();

                    foreach (var x in txt)
                    {
                        var y = end.Where(e => Path.GetFileNameWithoutExtension(e.Name).ToUpperInvariant() == Path.GetFileNameWithoutExtension(x.Name).ToUpperInvariant()).FirstOrDefault();
                        if (y != null)
                        {
                            _downloadList.Add(x);
                            _downloadList.Add(y);
                        }
                    }

                    isFileFound = _downloadList.Count > 0;

                    if (isFileFound)
                    {
                        // Download file to local via SFTP
                        foreach (var file in _downloadList)
                        {
                            DownloadFile(sftp, file, localPath);
                        }

                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Download Files Via FTP").ToSuccessLogString());
                    }
                    else
                    {
                        Logger.Info(_logMsg.Clear().SetPrefixMsg("Download Files Via FTP").Add("Error Message", "File Not Found").ToFailLogString());
                    }

                    sftp.Disconnect();
                }
                return(isFileFound);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occur:\n", ex);
                Logger.Info(_logMsg.Clear().SetPrefixMsg("Download Files Via FTP").Add("Error Message", ex.Message).ToInputLogString());
            }

            return(false);
        }