public Task <bool> MakeNewDir(string name) { return(Task.Run(() => { try { lock (persistentConnectionLock) { persistentConnection.MakeDirectory(name); } return true; } catch (FTPResponseException) { FTPInfoNotifier?.Invoke("无法新建文件夹 " + name); } return false; })); }
public Task <bool> RenameDirOrFile(string oldname, string newname) { return(Task.Run(() => { try { lock (persistentConnectionLock) { persistentConnection.ChangeFileOrDirName(oldname, newname); } return true; } catch (FTPResponseException) { FTPInfoNotifier?.Invoke("无法重命名文件 " + oldname); } return false; })); }
public Task <bool> DeleteFile(string name) { return(Task.Run(() => { try { lock (persistentConnectionLock) { persistentConnection.DeleteFile(name); } return true; } catch (FTPResponseException) { FTPInfoNotifier?.Invoke("无法删除文件 " + name); } return false; })); }
public Task <bool> ChangeWorkingDirectory(string newDirectory) { return(Task.Run(() => { try { lock (persistentConnectionLock) { persistentConnection.ChangeCurrentWorkingDirectory(newDirectory); CurrentRemotePath = persistentConnection.GetCurrentWorkingDirectory(); } return true; } catch (FTPResponseException) { FTPInfoNotifier?.Invoke("无法切换到目录 " + newDirectory); } 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; })); }
public void Connect() { try { cmdSocket.ReceiveTimeout = 10000; cmdSocket.SendTimeout = 10000; // 如 IIS FTP 服务端会在数据通道过多(大于2)时 Pending,故不设超时时间,用户可随时取消上传/下载任务 dataSocket.ReceiveTimeout = 0; dataSocket.SendTimeout = 0; FTPInfoNotifier?.Invoke("等待服务器响应..."); IAsyncResult result = null; try { result = cmdSocket.BeginConnect(new IPEndPoint(IPAddress.Parse(serverInfo.ServerIP), serverInfo.ServerPort), null, null); } catch (Exception ex) { throw new FTPResponseException(ex.Message); } if (result == null) { throw new FTPResponseException("无法建立连接。"); } result.AsyncWaitHandle.WaitOne(15000, true); if (!result.IsCompleted) { cmdSocket.Close(); throw new FTPResponseException("由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败"); } // 打开客户端接收数据的端口,端口号为客户端控制端口号+1 var localEndPoint = ((IPEndPoint)cmdSocket.LocalEndPoint); try { dataSocket.Bind(new IPEndPoint(IPAddress.Parse(localEndPoint.Address.ToString()), localEndPoint.Port + 1)); } catch (Exception) { throw new FTPResponseException("Data Socket 绑定失败", true); // 可能端口号+1被占用,允许自动重新创建 FTPConnection 以自动重试 } // 等待服务器响应就绪 220 var msg = WaitResponse(220); SendCommand("OPTS UTF8 ON"); msg = ReceiveRawResponse(); SendCommand("USER " + serverInfo.Username); var msgs = ReceiveResponse(331, 230); if (msgs.ContainsKey(331)) { SendCommand("PASS " + serverInfo.Password); try { msgs = ReceiveResponse(230); } catch (FTPResponseException) { } } if (!msgs.ContainsKey(230)) { // 密码错误 cmdSocket.Disconnect(true); throw new FTPResponseException("用户名或密码错误"); } FTPInfoNotifier?.Invoke("FTP 服务就绪"); } catch (SocketException ex) { throw new FTPResponseException(ex.Message); } catch (FormatException fe) { throw new FTPResponseException(fe.Message); } catch (ArgumentOutOfRangeException ee) { throw new FTPResponseException(ee.Message); } }