Example #1
0
        public static void GetFTPImages()
        {
            List<string> files = new List<string>();
            string FTP_Server = ConfigurationManager.AppSettings["FTP_Server"];
            string FTP_User = ConfigurationManager.AppSettings["FTP_User"];
            string FTP_Password = ConfigurationManager.AppSettings["FTP_Password"];
            string Monitor_Dir = ConfigurationManager.AppSettings["Monitor_Dir"];

            Directory.CreateDirectory(Monitor_Dir);

            FTP ftp = new FTP(FTP_Server, FTP_User, FTP_Password);
            files = ftp.Get_List();

            for (int i = 0; i <= files.Count - 1; i++)
            {
                files[i] = files[i].Replace(FTP_Server, "/");
                if (files[i].Contains("."))
                {
                    string source = FTP_Server + files[i].ToString();
                    string destination = (Monitor_Dir + files[i].ToString()).Replace("/", "\\");

                    string camera = "unknown";
                    string date = "unknown";
                    string time = "unknown";
                    var regex = new Regex(@"\\(.*)\\.*(2\d{7})(\d{6})");
                    var match = regex.Match(destination.Replace(Monitor_Dir, ""));
                    if (match.Success)
                    {
                        camera = match.Groups[1].Value;
                        date = match.Groups[2].Value;
                        time = match.Groups[3].Value;
                        destination = Monitor_Dir + "\\" + camera + "\\" + date + "\\" + time + "_" + i.ToString() + ".jpg";
                    }

                    Console.WriteLine("Downloading " + source + " to " + destination);
                    ftp.Download(source, destination);
                    AddTimestamp(destination, date + "_" + time);
                    ftp.Delete(source);
                }
            }
        }
Example #2
0
        public List<string> Get_List()
        {
            List<string> result = new List<string>();

            FtpWebRequest ftpRequest = (FtpWebRequest)WebRequest.Create(_FTP_Server);
            ftpRequest.Credentials = new NetworkCredential(_FTP_User, _FTP_Password);
            ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
            FtpWebResponse response = (FtpWebResponse)ftpRequest.GetResponse();

            StreamReader streamReader = new StreamReader(response.GetResponseStream());

            List<string> contents = new List<string>();

            string line = streamReader.ReadLine();
            while (!string.IsNullOrEmpty(line))
            {
                contents.Add(line);
                line = streamReader.ReadLine();
            }

            response.Close();
            streamReader.Close();

            for (int i = 0; i <= contents.Count - 1; i++)
            {
                if (contents[i].Contains("."))
                {
                    result.Add(_FTP_Server + contents[i]);
                }
                else
                {
                    //its a folder
                    FTP list = new FTP(_FTP_Server + contents[i] + "/", _FTP_User, _FTP_Password);
                    result.AddRange(list.Get_List());
                }
            }

            return result;
        }