Exemplo n.º 1
0
        /// <summary>
        /// Creates the file in the FtpClient host
        /// </summary>
        /// <param name="path">The path String.</param>
        /// <param name="bytes">The bytes.</param>
        /// <returns>true if it succeeds, false if it fails.</returns>
        /// <exception cref="FTPException"></exception>
        private bool CreateInternal(string path, byte[] bytes)
        {
            var result   = false;
            var attempts = 0;

            while (true)
            {
                attempts++;
                try
                {
                    LogConsumer.Info(Resources.FTP_CreateInternal, path.GetPathOrFileName());
                    var uri     = new Uri(path);
                    var request = (FtpWebRequest)WebRequest.Create(uri);
                    request.Credentials = new NetworkCredential(_userName, _password);
                    request.UsePassive  = true;
                    if (string.IsNullOrWhiteSpace(uri.GetFileExtension()) == false)
                    {
                        request.Method        = WebRequestMethods.Ftp.UploadFile;
                        request.ContentLength = bytes.Length;
                        var stream = request.GetRequestStream();
                        stream.Write(bytes, 0, bytes.Length);
                        stream.Close();
                    }
                    else
                    {
                        request.Method = WebRequestMethods.Ftp.MakeDirectory;
                    }

                    var response = (FtpWebResponse)request.GetResponse();
                    if (string.IsNullOrWhiteSpace(uri.GetFileExtension()) == false &&
                        response.StatusCode == FtpStatusCode.ClosingData ||
                        response.StatusCode == FtpStatusCode.PathnameCreated)
                    {
                        result = true;
                    }

                    response.Close();
                    break;
                }
                catch (WebException e)
                {
                    if (attempts >= 3)
                    {
                        throw new FTPException(path.GetPathOrFileName(), Resources.Create, e);
                    }

                    Thread.Sleep(1000);
                }
            }
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Check if the path/file exists in the FtpClient host.
        /// </summary>
        /// <param name="path">The path String.</param>
        /// <returns>true if it succeeds, false if it fails.</returns>
        /// <exception cref="InvalidOperationException"></exception>
        private bool ExistsInternal(string path)
        {
            var    result         = false;
            Stream responseStream = null;

            try
            {
                LogConsumer.Info(Resources.FTP_Exists_Checking, path.GetPathOrFileName());
                var uri     = new Uri(path);
                var request = (FtpWebRequest)WebRequest.Create(uri);
                request.Credentials = new NetworkCredential(_userName, _password);
                request.Method      = string.IsNullOrWhiteSpace(uri.GetFileExtension()) == false
                                     ? WebRequestMethods.Ftp.GetFileSize
                                     : WebRequestMethods.Ftp.ListDirectory;
                request.Timeout          = 30000;
                request.ReadWriteTimeout = 90000;
                request.UsePassive       = true;
                var response = (FtpWebResponse)request.GetResponse();
                var status   = response.StatusCode;
                responseStream = response.GetResponseStream();
                if (responseStream == null)
                {
                    throw new InvalidOperationException(Resources.ResponseStreamIsNull);
                }

                using var reader = new StreamReader(responseStream);
                responseStream   = null;
                while (reader.EndOfStream == false)
                {
                    _files.Enqueue(reader.ReadLine());
                }

                if (string.IsNullOrWhiteSpace(uri.GetFileExtension()) == false &&
                    status == FtpStatusCode.FileStatus ||
                    status == FtpStatusCode.OpeningData)
                {
                    result = true;
                }
            }
            catch (WebException)
            {
                result = false;
            }
            finally
            {
                responseStream?.Dispose();
            }
            return(result);
        }
Exemplo n.º 3
0
 /// <summary>
 /// Removes the path/file in the FtpClient host.
 /// </summary>
 /// <param name="path">The path String.</param>
 /// <returns>true if it succeeds, false if it fails.</returns>
 /// <exception cref="FTPException"></exception>
 /// <exception cref="FTPException"></exception>
 private void RemoveInternal(string path)
 {
     try
     {
         LogConsumer.Info(Resources.FTP_CreateInternal, path.GetPathOrFileName());
         var fullPath = new Uri(path);
         var request  = (FtpWebRequest)WebRequest.Create(fullPath);
         request.Credentials = new NetworkCredential(_userName, _password);
         request.Method      = string.IsNullOrWhiteSpace(fullPath.GetFileExtension()) == false ? WebRequestMethods.Ftp.DeleteFile : WebRequestMethods.Ftp.RemoveDirectory;
         request.UsePassive  = true;
         var response = (FtpWebResponse)request.GetResponse();
         if (response.StatusCode != FtpStatusCode.FileActionOK)
         {
             throw new FTPException(path.GetPathOrFileName(), Resources.Remove, response.StatusCode);
         }
     }
     catch (WebException e)
     {
         throw new FTPException(path.GetPathOrFileName(), Resources.Remove, e);
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// Handles the specified arguments.
 /// </summary>
 /// <param name="args">The arguments.</param>
 public void Handle(TestDoneEvent args)
 {
     LogConsumer.Info(@"Sample done action handled: {0} - {1} - {2:dd/MM/yyyy HH:mm:ss}", args.Identifier, args.Text, args.CreatedDateTIme);
 }