Exemplo n.º 1
0
        public ArrayList listarFTP(string ruta)
        {
            ArrayList Listado = new ArrayList();

            Tamir.SharpSsh.Sftp sftp = new Tamir.SharpSsh.Sftp(servidor, usuario, password);
            sftp.Connect();

            Listado = sftp.GetFileList(ruta);

            sftp.Close();
            return(Listado);
        }
Exemplo n.º 2
0
        public string[] Dir(string subFolder)
        {
            var result = new string[0];

            try
            {
                var files = sftp.GetFileList(subFolder);
                result = new string[files.Count];
                var i = 0;
                foreach (string file in files)
                {
                    result[i] = file;
                    i++;
                }
            }
            catch (Exception exception)
            {
                Debug.Write(exception.Message);
            }

            return(result);
        }
Exemplo n.º 3
0
		public List<string> GetFileList(EnumSettingSource settingSource, string settingKey, string directory, string filePattern)
		{
			var settings = this.ConnectionSettingsManager.Load<FtpConnectionSettings>(settingSource, settingKey); 
			var returnList = new List<string>();
			if(string.IsNullOrEmpty(filePattern))
			{
				filePattern = "*.*";
			}
			if(settings.SecureFTP)
			{
				string path;
				if (string.IsNullOrEmpty(directory))
				{
					path = filePattern;
				}
				else if (directory.EndsWith("/"))
				{
					path = directory + filePattern;
				}
				else
				{
					path = directory + "/" + filePattern;
				}
				this.EventReporter.Trace("Searching on " + settings.FtpHost + " for: " + path);
				Tamir.SharpSsh.Sftp ftp = new Tamir.SharpSsh.Sftp(settings.FtpHost, settings.FtpUserName, settings.FtpPassword);
				ftp.Connect();
				var list = ftp.GetFileList(path);
				this.EventReporter.Trace((list ?? new ArrayList()).Count.ToString() + " records found for " + path + " on " + settings.FtpHost);
				foreach (string fileName in list)
				{
					returnList.Add(fileName);
				}
			}
			else 
			{
				string path;
				if (string.IsNullOrEmpty(directory))
				{
					path = filePattern;
				}
				else if (directory.EndsWith("/"))
				{
					path = directory + filePattern;
				}
				else
				{
					path = directory + "/" + filePattern;
				}
				string targetUrl = new Uri(new Uri(Uri.UriSchemeFtp + "://" + settings.FtpHost), path).ToString();
				FtpWebRequest request = (FtpWebRequest)WebRequest.Create(targetUrl);
				request.Method = WebRequestMethods.Ftp.ListDirectory;

				request.Credentials = new NetworkCredential(settings.FtpUserName, settings.FtpPassword);

				this.EventReporter.Trace(string.Format("Searching on {0} for: {1}", targetUrl, path));

				string responseData;
				using(var response = (FtpWebResponse)request.GetResponse())
				using(var responseStream = response.GetResponseStream())
				using(var reader = new StreamReader(responseStream))
				{
					responseData = reader.ReadToEnd();
				}
				using(var reader = new StringReader(responseData))
				{
					string item;
					while((item = reader.ReadLine()) != null)
					{
						string filePath;
						//if (string.IsNullOrEmpty(directory))
						//{
							filePath = item;
						//}
						//else if (directory.EndsWith("/"))
						//{
						//	filePath = directory + item;
						//}
						//else
						//{
						//	filePath = directory + "/" + item;
						//}
						if (!returnList.Contains(filePath, StringComparer.CurrentCultureIgnoreCase))
						{
							returnList.Add(filePath);
						}
					}
				}

				this.EventReporter.Trace(string.Format("{0} records found for {1} on {2}, found {3}", returnList.Count, path, targetUrl, responseData));

				//http://stackoverflow.com/questions/652037/how-do-i-check-if-a-filename-matches-a-wildcard-pattern
			}
			return returnList;
		}
Exemplo n.º 4
0
        public List <string> GetFileList(EnumSettingSource settingSource, string settingKey, string directory, string filePattern)
        {
            var settings   = this.ConnectionSettingsManager.Load <FtpConnectionSettings>(settingSource, settingKey);
            var returnList = new List <string>();

            if (string.IsNullOrEmpty(filePattern))
            {
                filePattern = "*.*";
            }
            if (settings.SecureFTP)
            {
                string path;
                if (string.IsNullOrEmpty(directory))
                {
                    path = filePattern;
                }
                else if (directory.EndsWith("/"))
                {
                    path = directory + filePattern;
                }
                else
                {
                    path = directory + "/" + filePattern;
                }
                this.EventReporter.Trace("Searching on " + settings.FtpHost + " for: " + path);
                Tamir.SharpSsh.Sftp ftp = new Tamir.SharpSsh.Sftp(settings.FtpHost, settings.FtpUserName, settings.FtpPassword);
                ftp.Connect();
                var list = ftp.GetFileList(path);
                this.EventReporter.Trace((list ?? new ArrayList()).Count.ToString() + " records found for " + path + " on " + settings.FtpHost);
                foreach (string fileName in list)
                {
                    returnList.Add(fileName);
                }
            }
            else
            {
                string path;
                if (string.IsNullOrEmpty(directory))
                {
                    path = filePattern;
                }
                else if (directory.EndsWith("/"))
                {
                    path = directory + filePattern;
                }
                else
                {
                    path = directory + "/" + filePattern;
                }
                string        targetUrl = new Uri(new Uri(Uri.UriSchemeFtp + "://" + settings.FtpHost), path).ToString();
                FtpWebRequest request   = (FtpWebRequest)WebRequest.Create(targetUrl);
                request.Method = WebRequestMethods.Ftp.ListDirectory;

                request.Credentials = new NetworkCredential(settings.FtpUserName, settings.FtpPassword);

                this.EventReporter.Trace(string.Format("Searching on {0} for: {1}", targetUrl, path));

                string responseData;
                using (var response = (FtpWebResponse)request.GetResponse())
                    using (var responseStream = response.GetResponseStream())
                        using (var reader = new StreamReader(responseStream))
                        {
                            responseData = reader.ReadToEnd();
                        }
                using (var reader = new StringReader(responseData))
                {
                    string item;
                    while ((item = reader.ReadLine()) != null)
                    {
                        string filePath;
                        //if (string.IsNullOrEmpty(directory))
                        //{
                        filePath = item;
                        //}
                        //else if (directory.EndsWith("/"))
                        //{
                        //	filePath = directory + item;
                        //}
                        //else
                        //{
                        //	filePath = directory + "/" + item;
                        //}
                        if (!returnList.Contains(filePath, StringComparer.CurrentCultureIgnoreCase))
                        {
                            returnList.Add(filePath);
                        }
                    }
                }

                this.EventReporter.Trace(string.Format("{0} records found for {1} on {2}, found {3}", returnList.Count, path, targetUrl, responseData));

                //http://stackoverflow.com/questions/652037/how-do-i-check-if-a-filename-matches-a-wildcard-pattern
            }
            return(returnList);
        }