/// <summary> /// main method used to process the files every configurable interval /// </summary> public void ProcessFileControlThread() { string theFileToGrab = String.Empty; // main processing thread while (true) { try { using (FtpLib.FtpConnection ftp = new FtpLib.FtpConnection(m_FTPFolder, m_FTPUserName, m_FTPPwd)) { try { ftp.Open(); /* Open the FTP connection */ ftp.Login(); /* Login using previously provided credentials */ ftp.SetCurrentDirectory(m_FTPServerDirectory); /* change current directory */ FtpLib.FtpFileInfo[] flist = ftp.GetFiles(); foreach (FtpLib.FtpFileInfo fi in flist) { if (!_dbMgr.CheckDbBeforeDownloadingFile(fi.Name)) { try { string fil = m_FTPServerDirectory + fi.Name; if (ftp.FileExists(fil)) /* check that a file exists */ { ftp.GetFile(fil, m_FtpFileToLocation + fi.Name, false); FileWriter.Instance.WriteToLogFile("FtpHandler::ProcessFileControlThread:DownloadedFile" + fi.Name); // update the db so we do not download again _dbMgr.UpdateFtpFileNameInDb(fi.Name); } } catch (SystemException se) { FileWriter.Instance.WriteToLogFile("FtpHandler::ProcessControlThread:EXCEPTIONCAUGHT(Open/Login/Directory)::" + se.Message); FileWriter.Instance.WriteToLogFile(se.StackTrace); } } } } catch (SystemException se) { FileWriter.Instance.WriteToLogFile("FtpHandler::ProcessFileControlThread:EXCEPTIONCAUGHT::" + se.Message); } } } catch (SystemException se) { FileWriter.Instance.WriteToLogFile("FtpHandler::ProcessFileControlThread:EXCEPTIONCAUGHT::TryingToConnect" + se.Message); } // wait for the next interval to ftp the next file over System.Threading.Thread.Sleep(m_runIntervalInSeconds); } // while(true) } //ProcessFileControlThread
public void RecursiveDeleteFTP(string path, bool RemFromLog, ref FtpConnection ftpcon) { ftpcon.SetCurrentDirectory(path); foreach (FtpFileInfo fi in ftpcon.GetFiles()) { string fpath = string.Format("{0}/{1}", path, fi.Name); ftpcon.RemoveFile(fpath); Log.Write("Gon'delete: {0}", fpath); if (RemFromLog) RemoveFromLog(fpath); } foreach (FtpDirectoryInfo di in ftpcon.GetDirectories()) { if (di.Name != "." && di.Name != "..") { string fpath = string.Format("{0}/{1}", noSlashes(path), di.Name); Log.Write("Gon'delete files in: {0}", fpath); RecursiveDeleteFTP(fpath, RemFromLog, ref ftpcon); } } ftpcon.RemoveDirectory(path); if (RemFromLog) RemoveFromLog(path); }
/// <summary> /// main method used to process the files every configurable interval /// </summary> public void ProcessFileControlThread() { string theFileToGrab = String.Empty; // main processing thread while (true) { try { using (FtpLib.FtpConnection ftp = new FtpLib.FtpConnection(m_FTPFolder, m_FTPUserName, m_FTPPwd)) { try { ftp.Open(); /* Open the FTP connection */ ftp.Login(); /* Login using previously provided credentials */ ftp.SetCurrentDirectory(m_FTPServerDirectory); /* change current directory */ FtpLib.FtpFileInfo[] flist = ftp.GetFiles(); foreach (FtpLib.FtpFileInfo fi in flist) { if (!_dbMgr.CheckDbBeforeDownloadingFile(fi.Name)) { try { string fil = m_FTPServerDirectory + fi.Name; if (ftp.FileExists(fil)) /* check that a file exists */ { ftp.GetFile(fil, m_FtpFileToLocation + fi.Name, false); FileWriter.Instance.WriteToLogFile("FtpHandler::ProcessFileControlThread:DownloadedFile" + fi.Name); // update the db so we do not download again _dbMgr.UpdateFtpFileNameInDb(fi.Name); } } catch (SystemException se) { FileWriter.Instance.WriteToLogFile("FtpHandler::ProcessControlThread:EXCEPTIONCAUGHT(Open/Login/Directory)::" + se.Message); FileWriter.Instance.WriteToLogFile(se.StackTrace); } } } } catch (SystemException se) { FileWriter.Instance.WriteToLogFile("FtpHandler::ProcessFileControlThread:EXCEPTIONCAUGHT::" + se.Message); } } } catch (SystemException se) { FileWriter.Instance.WriteToLogFile("FtpHandler::ProcessFileControlThread:EXCEPTIONCAUGHT::TryingToConnect" + se.Message); } // wait for the next interval to ftp the next file over System.Threading.Thread.Sleep(m_runIntervalInSeconds); }// while(true) }
private static void RemoveDirectoryEvenIfNotEmpty(FtpConnection ftpConnection, string startDirectory) { if (!startDirectory.EndsWith("/")) startDirectory += "/"; List<string> directories = new List<string>(); directories.Add(startDirectory); int index = 0; while (index < directories.Count) { string currentDirectory = directories[index]; directories.AddRange(ftpConnection.GetDirectories(currentDirectory).Select(dir => currentDirectory + dir.Name + "/")); index++; } directories.Reverse(); List<string> fileNames = new List<string>(); foreach (var directory in directories) { foreach (var ftpFile in ftpConnection.GetFiles(directory)) { string fullPath = directory + ftpFile.Name; if (ftpConnection.FileExists(fullPath)) ftpConnection.RemoveFile(fullPath); } foreach (var ftpDir in ftpConnection.GetDirectories(directory)) { string fullPath = directory + ftpDir.Name; if (ftpConnection.DirectoryExists(fullPath)) ftpConnection.RemoveDirectory(fullPath); } } if (startDirectory != "./" && ftpConnection.DirectoryExists(startDirectory)) ftpConnection.RemoveDirectory(startDirectory); }
private void GetFilesByFtp(string host, string remoteLocation) { List<string> remoteFiles = new List<string>(); string localDownloadDirectory = string.Empty; FtpConnection connection = new FtpConnection(host, "mxftp", "mxftp"); try { try { connection.Open(); } catch (Exception ex) { CommonUtils.ShowError(ex.Message, ex); } connection.Login(); connection.SetCurrentDirectory(remoteLocation); FtpFileInfo[] ftpFileInfo = connection.GetFiles("*act.rftvp"); ScriptName = connection.GetCurrentDirectoryInfo().Name; localDownloadDirectory = downloadDir + ScriptName; //if(Directory.Exists(localDownloadDirectory)){ // Directory.Delete(localDownloadDirectory,true); // } DirectoryInfo downloadDirectoryInfo = Directory.CreateDirectory(localDownloadDirectory); connection.SetLocalDirectory(downloadDirectoryInfo.FullName); for (int i = 0; i < ftpFileInfo.Length; i++) { connection.GetFile(ftpFileInfo[i].Name, false); } } finally { connection.SetLocalDirectory(Path.GetTempPath()); connection.Close(); connection.Dispose(); } ReplaceFiles(localDownloadDirectory, txtDest.Text); //deleting local files //Directory.Delete(localDownloadDirectory); }