/// <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) { Console.WriteLine("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 { // get the directory listing List <string> dirList = FtpUtils.ListDirectoryOnFtpSite(m_uri, m_FTPUserName, m_FTPPwd); // only get the latest file for now loop thru all files // 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 dirList) { try { if (!m_db.CheckDbForFileDownloaded(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; string omcCdrs = FtpUtils.GetFileFromSite(u, m_FTPUserName, m_FTPPwd); // write the CDR file to our server WriteCdrFile(fileName, omcCdrs); // update the db so we do not download again UpdateDateFileDownloaded(fileName); // log the event so we know things are working this.LogFileMsg("OmcFtpHandler::ProcessFileControlThread()::FileFTP'dToS8Platform:" + fileName); } else { // log the event so we know things are working // this.LogFileMsg("OmcFtpHandler::ProcessFileControlThread()::File*ALREADY*FTP'dToS8Platform:" + fileName); } } catch (SystemException ex) { LogIt("OmcFtpHandler::ProcessFileControlThread():ExceptionCaughtProcessingFile:" + ex.Message); } } // file is downloaded so we can disconnect // m_ftplib.Disconnect(); } catch (System.Exception ex) { LogIt("OmcFtpHandler::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) { LogIt("OmcFtpHandler::Service is stopping -- ProcessFileControlThread is shutting down"); } catch (System.Exception ex) { LogIt("OmcFtpHandler::Exception Caught:" + ex.Message); } // catch } //ProcessFileControlThread