My requirement is to scan an FTP directory for the presence of 4 files and if a specific file exists I can begin to GET those files, read the contents and save the contents into an SQL 2005 DB.
The files are of a fixed length format which map to certain elements of data that my database will exploit. If i can learn to read 1 file and put the contents into the DB then I can do the other 3 on my own.
So far I can connect to the FTP and do a GET but I do not know the solution to my problem but I do know it will be written in C# and I do not want to save the file I GET from the FTP area before I read the contents.
Please can you give me some ideas or ask me to clarify further.
See attached code i am using:
Expand|Select|Wrap|Line Numbers
- using System;
- using System.IO;
- using System.Collections.Specialized;
- using com.enterprisedt.net.ftp;
- namespace AA.Renewals
- {
- public enum FTPHelperStatus {Initialising, Ready, Busy, No_Service, No_Settings, Not_Connected, Success, Error}
- /// <summary>
- /// Summary description for FTPHelper.
- /// </summary>
- public class FTPHelper
- {
- #region "private properties"
- private string ftpTarget;
- private NameValueCollection ftpSettings = new NameValueCollection();
- private FTPHelperStatus status;
- private FTPClient ftpClient;
- private string[] _fileList;
- #endregion
- public FTPHelper(string ftpTargetKey)
- {
- SetFtpTarget(ftpTargetKey); // Causes the setter to (Re)Load the settings
- // on completion, status will be either "No_Settings" or "Not_Connected"
- }
- private void LoadSettingsForFTPTarget()
- {
- status = FTPHelperStatus.Initialising;
- ftpSettings.Clear();
- foreach(string key in ConfigurationHelper.AppSettings.AllKeys)
- {
- if (key.StartsWith(this.ftpTarget))
- {
- // just use the bits of key without the ftpTargetprefix
- string keyTail = key.Substring(ftpTarget.Length, key.Length-ftpTarget.Length);
- ftpSettings.Add(keyTail, ConfigurationHelper.AppSettings[key]);
- }
- }
- status = (ftpSettings.Count > 0) ? FTPHelperStatus.Not_Connected : FTPHelperStatus.No_Settings;
- }
- internal FTPClient FTPClient
- {
- // lazy instantiation
- get
- {
- if (ftpClient == null)
- {
- if (!string.IsNullOrEmpty(this.MachineName))
- {
- ftpClient = new FTPClient(this.MachineName);
- }
- else
- {
- ftpClient = new FTPClient(ConfigurationHelper.AppSettings["TestSAPFTPMachineName"]);
- }
- }
- return ftpClient;
- }
- }
- public bool IsReady
- {
- get
- {
- return status == FTPHelperStatus.Ready;
- }
- }
- public string Setting(string settingName)
- {
- string retVal = ftpSettings[settingName];
- return (retVal != null) ? retVal : "";
- }
- public NameValueCollection Settings
- {
- get
- {
- return ftpSettings;
- }
- }
- public void SetFtpTarget(string ftptargetKey)
- {
- string ftpTargetValue = ConfigurationHelper.AppSettings[ftptargetKey];
- if (ftpTargetValue != this.ftpTarget)
- {
- this.ftpTarget = ftpTargetValue;
- // must load the new settings
- LoadSettingsForFTPTarget();
- }
- }
- private void logger_OnLoggableEvent(object o, LogInfoEventArgs e)
- {
- // lbProgress.Items.Add(e.Message);
- // lbProgress.SelectedIndex = lbProgress.Items.Count - 1;
- // Application.DoEvents();
- }
- private void Login()
- {
- FTPClient.Login(this.UserName, this.Pwd);
- }
- // Added on Dec 05 2006 -- To retain the control object SocketControl
- private void Login(string machineName)
- {
- FTPClient.Login(this.UserName, this.Pwd, machineName);
- }
- public bool CheckAvailable()
- {
- bool available = false;
- status = FTPHelperStatus.Busy;
- Login(); // this will create FTPclient if necessary, then log in
- //string[] dirContents = FTPClient.Dir(".",); // hopefully will get contents of 'root' directory
- available = (FTPClient.System() != "");
- FTPClient.Quit();
- status = FTPHelperStatus.Not_Connected;
- return available;
- }
- public void SendOutBoundFileStream(MemoryStream mStream, string zipFileName)
- {
- SendOutBoundFileStream(mStream, zipFileName, false);
- }
- public void SendOutBoundFileStream(MemoryStream mStream, string zipFileName, bool test)
- {
- // create an instance of FTPClient, passing in the name of hte host, the time out
- // and the logger for updates
- if(mStream != null)
- {
- mStream.Position = 0;
- }
- status = FTPHelperStatus.Busy;
- Login(this.MachineName); // this will create FTPclient if necessary, then log in
- FTPClient.TransferType = FTPTransferType.BINARY;
- // change to the output directory
- try
- {
- if( !test )
- {
- FTPClient.Chdir(this.OutFolder);
- status = FTPHelperStatus.Success;
- }
- else
- {
- FTPClient.Chdir(this.TestFolder);
- status = FTPHelperStatus.Success;
- }
- try
- {
- FTPClient.Put(mStream, zipFileName);
- status = FTPHelperStatus.Success;
- }
- catch
- {
- status = FTPHelperStatus.Error;
- }
- }
- catch
- {
- status = FTPHelperStatus.Error;
- }
- ftpClient.Quit();
- }
- public string[] ScanSource()
- {
- return ScanSource(".");
- }
- public string[] ScanSource(string dir)
- {
- try
- {
- status = FTPHelperStatus.Busy;
- Login(); // this will create FTPclient if necessary, then log in}
- if (dir != ".")
- {
- FTPClient.Chdir(dir);
- }
- _fileList = FTPClient.Dir();
- }
- catch (Exception oEx)
- {
- throw new Exception(string.Format("Error in AA.Renewals.FTPHelper.ScanSource(): ", oEx.Message), oEx);
- }
- finally
- {
- FTPClient.Quit();
- status = FTPHelperStatus.Not_Connected;
- }
- return _fileList;
- }
- public byte[] Get()
- {
- byte[] ObjFtp;
- try
- {
- status = FTPHelperStatus.Busy;
- Login();
- ObjFtp = FTPClient.Get("test.txt");
- }
- catch (Exception oEx)
- {
- throw new Exception(string.Format("Error in AA.Renewals.FTPHelper.Get(): {0}", oEx.Message), oEx);
- }
- finally
- {
- ftpClient.Quit();
- status = FTPHelperStatus.Not_Connected;
- }
- return ObjFtp;
- }
- #region "public properties"
- public FTPHelperStatus Status
- {
- get { return this.status; }
- set { this.status = value; }
- }
- public string MachineName
- {
- get
- {
- string retVal = ftpSettings["MachineName"];
- return (retVal != null) ? retVal : "";
- }
- }
- public string UserName
- {
- get
- {
- string retVal = ftpSettings["UserName"];
- return (retVal != null) ? retVal : "";
- }
- }
- public string Pwd
- {
- get
- {
- string retVal = ftpSettings["Pwd"];
- return (retVal != null) ? retVal : "";
- }
- }
- public string InFolder
- {
- get
- {
- string retVal = ftpSettings["In"];
- return (retVal != null) ? retVal : "";
- }
- }
- public string OutFolder
- {
- get
- {
- string retVal = ftpSettings["Out"];
- return (retVal != null) ? retVal : "";
- }
- }
- public string TestFolder
- {
- get
- {
- string retVal = ftpSettings["Test"];
- return (retVal != null) ? retVal : "";
- }
- }
- #endregion
- }
- }