private static void setProxyInfo(SetupConf setVariables, FtpWebRequest request)
        {
            try
            {
                string strProxyServer = setVariables.objProxyVariables.proxyServer.ToString();
                string strProxyPort = setVariables.objProxyVariables.proxyPort.ToString();
                string strProxyUsername = setVariables.objProxyVariables.proxyUsername.ToString();
                string strProxyPassword = setVariables.objProxyVariables.proxyPassword.ToString();

                IWebProxy proxy = request.Proxy;
                if (proxy != null)
                {
                    Console.WriteLine("Proxy: {0}", proxy.GetProxy(request.RequestUri));
                }

                WebProxy myProxy = new WebProxy();
                Uri newUri = new Uri(Uri.UriSchemeHttp + "://" + strProxyServer + ":" + strProxyPort);
                // Associate the newUri object to 'myProxy' object so that new myProxy settings can be set.
                myProxy.Address = newUri;
                // Create a NetworkCredential object and associate it with the
                // Proxy property of request object.
                myProxy.Credentials = new NetworkCredential(strProxyUsername, strProxyPassword);
                request.Proxy = myProxy;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
                Console.ReadLine();
                System.Environment.Exit(-1);
            }
        }
 //this method saves the logfile.
 private static void saveNewServerLogFile(ArrayList alLog, SetupConf setVariables)
 {
     string strFormat = "MMM ddd d HH:mm yyyy";
     string strDate = System.DateTime.Now.ToString(strFormat);
     strDate = strDate.Replace(" ", "");
     strDate = strDate.Replace(":", "");
     try
     {
         using (StreamWriter outputFile = new StreamWriter(@"" + setVariables.objVaribles.Path + "access_log" + strDate + ".log"))
         {
             foreach (var item in alLog)
             {
                 outputFile.WriteLine(item.ToString());
                 outputFile.Flush();
             }
         }
         Console.WriteLine("Log file saved");
     }
     catch (Exception e)
     {
         Console.WriteLine("Log file could not be saved");
         Console.WriteLine(e.Message.ToString());
         Console.ReadLine();
         System.Environment.Exit(-1);
     }
 }
 public FtpApp()
 {
     SetupConf MyVariables = new SetupConf();
     MyVariables.setVariables();
     _timer = new System.Timers.Timer(MyVariables.objVaribles.Timer) { AutoReset = true };
     _timer.Elapsed += (sender, eventArgs) => PrepareApp(MyVariables);
 }
 private static void setProxyAuthorization(SetupConf setVariables, FtpWebRequest request)
 {
     //Proxy configurations
     if (setVariables.objProxyVariables.proxyServer.ToString() != "PlaceHolder" && setVariables.objProxyVariables.proxyPort.ToString() != "PlaceHolder")
     {
         setProxyInfo(setVariables, request);
     }
 }
 private static void PrepareApp(SetupConf objSetVariables)
 {
     //cheking that all the important ftp server information is present.
     if (objSetVariables.objVaribles.Url.Equals("") || objSetVariables.objVaribles.Username.Equals("") || objSetVariables.objVaribles.Password.Equals("") || objSetVariables.objVaribles.Path.Equals(""))
     {
         Console.WriteLine("Please provide the required information in the auto.conf file and restart the application.");
         Console.ReadLine();
         System.Environment.Exit(-1);
     }
     else
     {
         Console.WriteLine("starting processing step");
         startProcess(objSetVariables);
     }
 }
        static void startProcess(SetupConf setVariables)
        {
            ArrayList alLog = new ArrayList();
            try
            {
                Console.WriteLine("Creating webrequest");
                // Get the object used to communicate with the server.
                FtpWebRequest request = (FtpWebRequest)WebRequest.Create(setVariables.objVaribles.Url.ToString());

                //setting up proxy settings
                setProxyAuthorization(setVariables, request);

                request.Method = WebRequestMethods.Ftp.DownloadFile;
                // Assumes the FTP site uses anonymous logon.
                request.Credentials = new NetworkCredential(setVariables.objVaribles.Username.ToString(), setVariables.objVaribles.Password.ToString());
                Console.WriteLine("initiatin ftp webresponse \nPlease wait.....");
                using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
                using (Stream responseStream = response.GetResponseStream())
                using (StreamReader reader = new StreamReader(responseStream))
                {
                    string strText = "";
                    while ((strText = reader.ReadLine()) != null)
                    {
                        alLog.Add(strText);
                    }
                }

                Console.WriteLine("Log file has been retrieved");

                SaveLog objSaveLog = new SaveLog();
                ArrayList alNewData = new ArrayList();//create a new arrayList for the new data in the log
                EditLog objSortOldData = new EditLog();

                Console.WriteLine("Removing duplicates...");
                alNewData = objSortOldData.removeOldData(alLog, setVariables);

                Console.WriteLine("Saving the log file...");
                objSaveLog.save(alNewData, setVariables);

                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
                Console.ReadLine();
                System.Environment.Exit(-1);
            }
        }
        //try and catch.
        public ArrayList removeOldData(ArrayList alFtpLog, SetupConf setVariables)
        {
            try
            {
                int intCounter = 0;
                ArrayList alNewLog = new ArrayList();
                bool blnFoundLastRec = false;
                string strTemp = "";
                Console.WriteLine("Checking for old data.");
                foreach (var item in alFtpLog)
                {
                    strTemp = item.ToString();
                    strTemp = strTemp.Substring(strTemp.IndexOf("["), 28);
                    if (blnFoundLastRec)
                    {
                        alNewLog.Add(item);
                    }
                    if (strTemp == setVariables.objVaribles.LastRead.ToString() &&
                        (intCounter + 1).ToString() == setVariables.objVaribles.IndexOfLastRead.ToString())
                    {
                        blnFoundLastRec = true;
                    }
                    intCounter++;
                }

                strTemp = alFtpLog[alFtpLog.Count - 1].ToString();
                strTemp = strTemp.Substring(strTemp.IndexOf("["), 28);
                setVariables.objVaribles.LastRead = strTemp;
                setVariables.objVaribles.IndexOfLastRead = (alFtpLog.Count).ToString();

                if (!blnFoundLastRec)
                {
                    return alFtpLog;//this would mean that the server has rotated the log.

                }
                else
                {
                    return alNewLog;// this means that we found we read the last time the program ran.
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message.ToString());
                Console.ReadLine();
                System.Environment.Exit(-1);
            }
            return null;
        }
        //main method for controlling what goes on here.
        public void save(ArrayList alLog, SetupConf setVariables)
        {
            try
            {
                saveNewServerLogFile(alLog, setVariables);//calling the method to save the new logfile.

                saveNewLogLine(setVariables);//calling the method to save the last line we read from the log file.
            }
            catch (Exception e)
            {
                Console.WriteLine("Log file could not be saved");
                Console.WriteLine(e.Message.ToString());
                Console.ReadLine();
                System.Environment.Exit(-1);
            }
        }
 //this method saves the last log record we read.
 private static void saveNewLogLine(SetupConf setVariables)
 {
     try
     {
         using (StreamWriter outputFile = new StreamWriter(setVariables.objVaribles.ConfPath))
         {
             string[] strVariables = { "lastreadLine : \"" + setVariables.objVaribles.LastRead.ToString() + "\"", "indexOfLastReadLine : \"" + setVariables.objVaribles.IndexOfLastRead.ToString() + "\"" };
             foreach (var item in strVariables)
             {
                 outputFile.WriteLine(item.ToString());
                 outputFile.Flush();
             }
         }
         Console.WriteLine("new settings saved to config file");
     }
     catch (Exception e)
     {
         Console.WriteLine("new conf settings could not be saved");
         Console.WriteLine(e.Message.ToString());
         System.Environment.Exit(-1);
     }
 }