Пример #1
0
        /// <summary>
        /// Usage: ftpcp [options] source [user@]host:target
        ///
        /// Options:
        ///     -p port   connect to specified port
        ///     -l user   connect with specified username
        ///     -pw passw login with specified password
        ///     -a        anonymous connection
        ///     -pasv     use passive mode
        ///
        /// </summary>
        static int Main(string[] args)
        {
            ShowAppInfo();

            if (args.Length == 0)
            {
                ShowUsageDescription();
                return(0);
            }
            else if (!ParseParams(args))
            {
                ShowHelpMessage();
                return(-1);
            }

            if (!CheckSourceFile())
            {
                return(3);
            }

            int result = RequestCredentials();

            if (result != 0)
            {
                return(result);
            }

            FtpClientConfiguration configuration = new FtpClientConfiguration {
                Host          = host,
                BaseDirectory = path
            };

            if (port != 0)
            {
                configuration.Port = port;
            }
            if (anonymous)
            {
                configuration.Username = "******";
                configuration.Password = "******";
            }
            else
            {
                configuration.Username = username;
                configuration.Password = password;
            }
            if (pasvMode)
            {
                //TODO: set passive mode
            }

            Task task = TransferFileAsync(configuration);

            task.Wait();

            return(resultCode);
        }
Пример #2
0
        public IFtpClient Create(string ftpUri, Action <FtpClientConfiguration> ftp = null)
        {
            FtpClientConfiguration ftpClientConfiguration = new FtpClientConfiguration(ftpUri);

            if (ftp != null)
            {
                ftp(ftpClientConfiguration);
            }
            return(new FtpClient(ftpClientConfiguration));
        }
Пример #3
0
        public ListDirectoryProvider(FtpClient ftpClient, FtpClientConfiguration configuration)
        {
            this.ftpClient     = ftpClient;
            this.configuration = configuration;

            directoryParsers = new List <IListDirectoryParser>
            {
                new UnixDirectoryParser(),
                new DosDirectoryParser(),
            };
        }
        public IFtpClient Create(Uri ftpUri, Action <FtpClientConfiguration> ftp = null)
        {
            var configuration = new FtpClientConfiguration(ftpUri);

            if (ftp != null)
            {
                ftp(configuration);
            }

            return(new FtpClient(configuration));
        }
Пример #5
0
        private static Task TransferFileAsync(FtpClientConfiguration configuration)
        {
            return(Task.Run(async() => {
                using var client = new FtpClient(configuration);

                try {
                    await client.LoginAsync();
                } catch (Exception e) {
                    Console.WriteLine(e.Message);
                    resultCode = 1;
                    return;
                }

                FileInfo fileInfo = new FileInfo(source);

                Stream writeStream;
                try {
                    writeStream = await client.OpenFileWriteStreamAsync(fileInfo.Name);
                } catch (Exception e) {
                    Console.WriteLine(e.Message);
                    resultCode = 1;
                    return;
                }

                Stream readStream = null;
                try {
                    readStream = fileInfo.OpenRead();
                    await readStream.CopyToAsync(writeStream);
                } catch (Exception e) {
                    Console.WriteLine(e.Message);
                    resultCode = 1;
                    return;
                } finally {
                    readStream?.Dispose();
                    writeStream.Dispose();
                }

                Console.WriteLine($"{source}: upload success");
            }));
        }
Пример #6
0
 public MlsdDirectoryProvider(FtpClient ftpClient, ILogger logger, FtpClientConfiguration configuration)
 {
     this.ftpClient     = ftpClient;
     this.configuration = configuration;
     this.logger        = logger;
 }
Пример #7
0
 public FtpControlStream(FtpClientConfiguration configuration, IDnsResolver dnsResolver)
 {
     LoggerHelper.Debug("Constructing new FtpSocketStream");
     Configuration    = configuration;
     this.dnsResolver = dnsResolver;
 }
 public MlsdDirectoryProvider(FtpClient ftpClient, FtpClientConfiguration configuration)
 {
     this.ftpClient     = ftpClient;
     this.configuration = configuration;
 }
Пример #9
0
        public static void Upload(Stream file, string directoryUrl, string fileUrl, string userName, string password, bool?usePassive = false)
        {
            #region Folder

            //replace last slash
            fileUrl      = fileUrl.EndsWith("/") ? fileUrl.Substring(0, fileUrl.Length - 1) : fileUrl;
            directoryUrl = directoryUrl.EndsWith("/") ? directoryUrl.Substring(0, directoryUrl.Length - 1) : directoryUrl;

#if NET452
            // Set the ftp credentials
            var credential = new NetworkCredential(userName, password);

            // Create a ftp client
            var client = new FtpClient(credential);
#elif NETCOREAPP2_2
            // Set the ftp credentials
            var config = new FtpClientConfiguration
            {
                Username = userName,
                Password = password
            };
            // Create a ftp client
            var client = new FtpClient(config);
#endif

#if NET452
            // Check for folder existence
            if (!client.DirectoryExists(new Uri(directoryUrl)))
            {
#endif
            try
            {
                // Create the ftp request object
                FtpWebRequest ftpReq = (FtpWebRequest)FtpWebRequest.Create(new Uri(directoryUrl));
                ftpReq.Credentials = new NetworkCredential(userName, password);
                ftpReq.Method      = WebRequestMethods.Ftp.MakeDirectory;

                // Create the ftp response object
                FtpWebResponse ftpRes = (FtpWebResponse)ftpReq.GetResponse();
            }
            catch (Exception)
            {
            }
#if NET452
        }
#endif

            #endregion

            #region File

            // Create FtpWebRequest object from the Uri provided
            System.Net.FtpWebRequest ftpWebRequest = (System.Net.FtpWebRequest)System.Net.FtpWebRequest.Create(new Uri(fileUrl));

            // Provide the WebPermission Credintials
            ftpWebRequest.Credentials        = new System.Net.NetworkCredential(userName, password);
            ftpWebRequest.ImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.None;

            // set timeout for 20 seconds
            ftpWebRequest.Timeout = 20000;

            // set transfer mode
            ftpWebRequest.UsePassive = true;

            // Specify the command to be executed.
            ftpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;

            // Specify the data transfer type.
            ftpWebRequest.UseBinary = true;

            // Notify the server about the size of the uploaded file
            ftpWebRequest.ContentLength = file.Length;

            // The buffer size is set to 2kb
            int buffLength = 2048;
            byte[] buff    = new byte[buffLength];

            try
            {
                // Stream to which the file to be upload is written
                System.IO.Stream stream = ftpWebRequest.GetRequestStream();

                // Read from the file stream 2kb at a time
                int contentLen = file.Read(buff, 0, buffLength);

                // Till Stream content ends
                while (contentLen != 0)
                {
                    // Write Content from the file stream to the FTP Upload Stream
                    stream.Write(buff, 0, contentLen);
                    contentLen = file.Read(buff, 0, buffLength);
                }

                // Close the file stream and the Request Stream
                stream.Flush();
                stream.Close();
                stream.Dispose();
                file.Close();
                file.Dispose();

                FtpWebResponse ftpRes = (FtpWebResponse)ftpWebRequest.GetResponse();
            }
            catch (Exception)
            {
            }


            #endregion
        }