private void CloseConnection(FTPConnection ftpConnection) { if (ftpConnection.IsConnected) { ftpConnection.Close(); } }
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); } } } })); }
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; })); }
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; })); }
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; })); }