コード例 #1
0
        public FTPConnection Connect()
        {
            FTPConnection ftpConnection = new FTPConnection();

            try
            {
                ftpConnection.ServerAddress = serverName;
                ftpConnection.ServerPort    = serverPort;
                ftpConnection.UserName      = (userName == string.Empty) ? "anonymous" : userName;
                ftpConnection.Password      = (userName == string.Empty) ? "1" : userPassword;
                ftpConnection.ConnectMode   = (passivMode) ? FTPConnectMode.PASV : FTPConnectMode.ACTIVE;
                ftpConnection.Connect();
                return(ftpConnection);
            }
            catch (FTPException)
            {
                CloseConnection(ftpConnection);
                throw;
            }
            catch (IOException)
            {
                CloseConnection(ftpConnection);
                throw;
            }
            catch (SocketException)
            {
                CloseConnection(ftpConnection);
                throw;
            }
        }
コード例 #2
0
ファイル: FTPService.cs プロジェクト: sevenaper/FTPclient
        private long connectionCount = 0;  // Note: 用于Connection命名,ConnectionName目前只用于日志

        private FTPConnection CreateConnection(ServerInfo server, bool setNotifier = false, string connectionName = null)
        {
            if (connectionName == null)
            {
                connectionCount++; connectionName = connectionCount.ToString();
            }
            int retryCount = 0;

            while (retryCount <= 3)
            {
                try
                {
                    retryCount++;
                    FTPConnection ftpConnection = new FTPConnection(connectionName, server);
                    if (setNotifier)
                    {
                        ftpConnection.FTPInfoNotifier += FTPInfoNotifier;
                    }
                    ftpConnection.Connect();
                    return(ftpConnection);
                }
                catch (FTPResponseException ex)
                {
                    if (!ex.Recoverable)
                    {
                        throw;
                    }
                }
            }
            throw new FTPResponseException("客户端无法创建Socket,请检查端口号使用情况");
        }
コード例 #3
0
 private void CloseConnection(FTPConnection ftpConnection)
 {
     if (ftpConnection.IsConnected)
     {
         ftpConnection.Close();
     }
 }
コード例 #4
0
ファイル: FTPService.cs プロジェクト: sevenaper/FTPclient
        private List <RemoteFile> ParseFileList(FTPConnection conn, string filelist)
        {
            List <RemoteFile> list = new List <RemoteFile>();

            string[] files = filelist.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var file in files)
            {
                RemoteFile remoteFile = new RemoteFile
                {
                    Size         = "",
                    ModifiedTime = ""
                };
                var size = conn.GetFileSize(file);
                if (size == -1)
                {
                    remoteFile.IsDirectory = true; remoteFile.Name = file;
                }
                else
                {
                    var rawTime = conn.GetFileLastModifiedTime(file);
                    try
                    {
                        remoteFile.ModifiedTime = DateTime.ParseExact(rawTime, "yyyyMMddHHmmss", CultureInfo.CurrentCulture).ToString("yyyy/MM/dd HH:mm:ss");
                        remoteFile.Size         = Utils.SizeToFriendlyString(size);
                    }
                    catch (Exception) { }
                    remoteFile.IsDirectory = false; remoteFile.Name = file;
                }
                list.Add(remoteFile);
            }
            return(list);
        }
コード例 #5
0
        private bool DeleteDir(string name, FTPConnection ftpConnection)
        {
            try
            {
                ftpConnection.ChangeCurrentWorkingDirectory(name);

                string result = ftpConnection.GetFileList();
                var    list   = ParseFileList(ftpConnection, result);
                foreach (var file in list)
                {
                    if (!file.IsDirectory)
                    {
                        ftpConnection.DeleteFile(file.Name);
                    }
                    else
                    {
                        var ret = DeleteDir(name + "/" + file.Name, ftpConnection);
                    }
                }

                ftpConnection.DeleteDir(name);

                return(true);
            }
            catch (FTPResponseException ex)
            {
                FTPErrorNotifications?.Invoke(ex);
            }
            return(false);
        }
コード例 #6
0
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            ConnectInfo connectInfo = new ConnectInfo();

            try
            {
                connectInfo.ServerName   = textBoxHost.Text;
                connectInfo.ServerPort   = textBoxPort.Text;
                connectInfo.UserName     = textBoxUser.Text;
                connectInfo.UserPassword = textBoxPassword.Password;
                connectInfo.PassivMode   = (checkBoxPassivMode.IsChecked == true) ? true : false;
                try
                {
                    ftpConnection = connectInfo.Connect();
                    connected     = ftpConnection.IsConnected;
                    connectInfo.Save();
                }
                catch (FTPException exception)
                {
                    MessageBox.Show(exception.Message, "Ошибка " + exception.ReplyCode, MessageBoxButton.OK, MessageBoxImage.Error);
                }
                catch (IOException exception)
                {
                    MessageBox.Show(exception.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                catch (SocketException exception)
                {
                    MessageBox.Show(exception.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            catch (FormatException exception)
            {
                MessageBox.Show(exception.Message, "Ошибка", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }
コード例 #7
0
 public Task <bool> DeleteDirWithFiles(string name)
 {
     return(Task.Run(() =>
     {
         try
         {
             FTPConnection ftpConnection = CreateConnection(Server);
             var result = DeleteDir(name, ftpConnection);
             ftpConnection.Close();
             return result;
         }
         catch (FTPResponseException ex)
         {
             FTPErrorNotifications?.Invoke(ex);
         }
         return false;
     }));
 }
コード例 #8
0
ファイル: FTPService.cs プロジェクト: sevenaper/FTPclient
 public Task UploadFile(FileUploadingInfo fileUploadingInfo, CancellationToken token, FTPTaskErrorHandler ftpTaskErrorHandler = null)
 {
     return(Task.Run(() =>
     {
         lock (fileUploadingInfo)
         {
             if (fileUploadingInfo.IsFinished)
             {
                 return;
             }
             FTPConnection ftpConnection = null;
             try
             {
                 ftpConnection = CreateConnection(fileUploadingInfo.serverInfo);
                 Utils.WriteDebugInfo(ftpConnection.connectionName, "用于上传文件的连接已建立。\r\nFileUploadingInfo: " + fileUploadingInfo.ToString());
                 ftpConnection.UploadFile(fileUploadingInfo, token);
                 ftpConnection.Close();
                 fileUploadingInfo.Close();
                 fileUploadingInfo.IsFinished = true;
                 Utils.WriteDebugInfo(ftpConnection.connectionName, "操作已完成,连接主动断开。");
             }
             catch (OperationCanceledException)
             {
                 ftpConnection?.Close();
                 if (ftpConnection != null)
                 {
                     Utils.WriteDebugInfo(ftpConnection.connectionName, "操作被用户取消,连接已断开。");
                 }
             }
             catch (FTPResponseException ex)
             {
                 ftpTaskErrorHandler?.Invoke(ex, fileUploadingInfo.Tag);
                 ftpConnection?.Close();
                 if (ftpConnection != null)
                 {
                     Utils.WriteDebugInfo(ftpConnection.connectionName, "由于发生了不可恢复的异常,连接已断开。" + " FTPResponseException: " + ex.Message);
                 }
             }
         }
     }));
 }
コード例 #9
0
ファイル: FTPService.cs プロジェクト: sevenaper/FTPclient
 public Task <bool> CreatePersistentConnection()
 {
     return(Task.Run(() =>
     {
         try
         {
             lock (persistentConnectionLock)
             {
                 persistentConnection?.Close();
                 persistentConnection = CreateConnection(Server, true);
                 CurrentRemotePath = persistentConnection.GetCurrentWorkingDirectory();
             }
             timerKeepAlive = new Timer((state) =>
             {
                 try
                 {
                     lock (persistentConnectionLock)
                     {
                         persistentConnection.SendNullCommand();
                     }
                 }
                 catch (FTPResponseException ex)
                 {
                     timerKeepAlive.Dispose();
                     timerKeepAlive = null;
                     FTPErrorNotifications?.Invoke(ex);
                 }
             }, null, 15000, 15000);
             return true;
         }
         catch (FTPResponseException ex)
         {
             FTPErrorNotifications?.Invoke(ex);
         }
         return false;
     }));
 }
コード例 #10
0
ファイル: FTPService.cs プロジェクト: sevenaper/FTPclient
 public Task <List <RemoteFile> > GetFileList()
 {
     return(Task.Run(() =>
     {
         FTPConnection ftpConnection = null;
         try
         {
             ftpConnection = CreateConnection(Server);
             Utils.WriteDebugInfo(ftpConnection.connectionName, "用于获取文件列表的连接已建立。");
             ftpConnection.ChangeCurrentWorkingDirectory(CurrentRemotePath);
             FTPInfoNotifier?.Invoke("获取文件列表...");
             string result = ftpConnection.GetFileList(true);
             var list = ParseFileListWithFileInfo(result);
             if (list == null)
             {
                 // fallback to NLST
                 result = ftpConnection.GetFileList(false);
                 list = ParseFileList(ftpConnection, result);
             }
             FTPInfoNotifier?.Invoke("FTP 服务就绪,文件列表刷新于 " + DateTime.Now + ",共" + list.Count + "项");
             ftpConnection.Close();
             Utils.WriteDebugInfo(ftpConnection.connectionName, "操作已成功完成,连接主动断开。");
             return list;
         }
         catch (FTPResponseException ex)
         {
             FTPErrorNotifications?.Invoke(ex);
             ftpConnection?.Close();
             if (ftpConnection != null)
             {
                 Utils.WriteDebugInfo(ftpConnection.connectionName, "由于发生了不可恢复的异常,连接已断开。" + " FTPResponseException: " + ex.Message);
             }
         }
         return null;
     }));
 }