Exemple #1
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);
        }
Exemple #2
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;
     }));
 }
Exemple #3
0
 public Task <bool> ReturnToParentDirectory()
 {
     return(Task.Run(() =>
     {
         try
         {
             lock (persistentConnectionLock)
             {
                 persistentConnection.ChangeCurrentWorkingDirectory("..");
                 CurrentRemotePath = persistentConnection.GetCurrentWorkingDirectory();
             }
             return true;
         }
         catch (FTPResponseException ex)
         {
             FTPErrorNotifications?.Invoke(ex);
         }
         return false;
     }));
 }
Exemple #4
0
 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;
     }));
 }
Exemple #5
0
 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;
     }));
 }