/// <summary> /// method to get a directory listing of the FTP site /// </summary> /// <param name="ftpUri"></param> /// <returns></returns> public static List <string> ListDirectoryOnFtpSite(Uri ftpUri, string user, string pass) { string dirListing = string.Empty; List <string> dirList = new List <string>(); // The serverUri parameter should use the ftp:// scheme. // It contains the name of the server file that is to be deleted. // Example: ftp://contoso.com/someFile.txt. // try { if (ftpUri.Scheme != Uri.UriSchemeFtp) { return(dirList); } // Get the object used to communicate with the server. FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftpUri); NetworkCredential nc = new NetworkCredential(user, pass); CredentialCache cc = new CredentialCache(); cc.Add(ftpUri, "Basic", nc); request.Credentials = cc; // get the FTP directory list to determine new file to download request.Method = WebRequestMethods.Ftp.ListDirectoryDetails; FtpWebResponse response = (FtpWebResponse)request.GetResponse(); StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.ASCII); // maybe read each line here and get the latest file dirListing = sr.ReadToEnd(); dirList = FtpUtils.ParseDirectoryList(dirListing); response.Close(); } catch (SystemException se) { m_logger.WriteToLogFile("ECaught:" + se.Message + se.StackTrace); } return(dirList); }
/// <summary> /// main method used to process the files every configurable interval /// </summary> protected void ProcessFileControlThread() { try { // main processing thread while (true) { try { // check to see if we have a file, if so we process it, if not we create a // dummy file to upload (an add and a delete file) // get the local directory listing List <string> dirList = FtpUtils.ListDirectoryOnFtpSite(m_uri, m_FTPUserName, m_FTPPwd); // only get the latest file for now loop thru all files // Only get files that our csv format "*.csv" string[] dirs = Directory.GetFiles(this.m_TNLocalDirectory, "*.csv"); // add code here to verify that the file has not already been downloaded // all downloaded files will get processed into the database // if (theFileToGrab) is not in db // download the file foreach (string fileName in dirs) { try { if (!m_db.CheckDbForSyniverseFileUploaded(fileName)) { // here we download directly to the directory // where the CDRHandler will process it // m_ftplib.OpenDownload(theFileToGrab, m_FtpFileToLocation + theFileToGrab, true); UriBuilder ub = new UriBuilder(m_FTPServerDirectory + @"/" + fileName); Uri u = ub.Uri; byte[] b = FtpUtils.PutFileToSite(u, fileName, m_FTPUserName, m_FTPPwd); // update the db so we do not download again this.m_db.UpdateDateFileUploaded(fileName); // log the event so we know things are working this.LogFileMsg("FtpHandler::ProcessFileControlThread()::FileFTP'dToSyniverse:" + fileName); } else { // log the event so we know things are working // this.LogFileMsg("FtpHandler::ProcessFileControlThread()::File*ALREADY*FTP'dToS8Platform:" + fileName); } } catch (SystemException ex) { m_logger.WriteToEventLog("FtpHandler::ProcessFileControlThread():ExceptionCaughtProcessingFile:" + fileName + ex.Message); } } // file is downloaded so we can disconnect // m_ftplib.Disconnect(); } catch (System.Exception ex) { m_logger.WriteToEventLog("FtpHandler::ProcessFileControlThread():Exception Caught:" + ex.Message); return; } // wait for the next interval to ftp the next file over System.Threading.Thread.Sleep(m_runIntervalInSeconds); }// while(true) } catch (System.Threading.ThreadAbortException) { m_logger.WriteToEventLog("FtpHandler::Service is stopping -- ProcessFileControlThread is shutting down"); } catch (System.Exception ex) { m_logger.WriteToEventLog("FtpHandler::Exception Caught:" + ex.Message); } // catch } //ProcessFileControlThread