private static Task DeleteFile(IFtpClient client, string remoteFilePath) { return(Task.Run(() => { client.DeleteFile(remoteFilePath); })); }
public void Remove(string Path, string Filename) { try { if (_ftpClient.DirectoryExists(Path)) { _ftpClient.RetryAttempts = 10; string fullpath = Path + Filename; _log.Info($"{FtpTemplateMessage("Remove", Filename)}", fullpath); _ftpClient.DeleteFile(fullpath); } else { _log.Info($"{FtpTemplateMessage("Remove", Filename, "not exist")}", Path + Filename); throw new Exception("File Not Exist"); } } catch (Exception ex) { _log.Error($"{FtpTemplateMessage("Remove", Filename, "throw exception")}", ex); throw ex; } }
private void SyncDirectory(IFtpClient client, string remotePath, string localPath, bool isDel) { var localFolder = new DirectoryInfo(localPath); var infos = localFolder.GetFileSystemInfos(); foreach (var info in infos) { if (!client.IsConnected) { client.Connect(); } if (info is FileInfo) { var size = (info as FileInfo).Length; var remoteFile = Path.Combine(remotePath, info.Name); if (!client.FileExists(remoteFile) || client.GetFileSize(remoteFile) != size) { client.UploadFile(info.FullName, remoteFile); Logger.Info($"Uploaded==>{info.FullName}"); } } else if (info is DirectoryInfo) { var remoteFile = Path.Combine(remotePath, info.Name); if (!client.DirectoryExists(remoteFile)) { client.CreateDirectory(remoteFile); Logger.Info($"CreateFtpDirectory==>{remoteFile}"); } SyncDirectory(client, Path.Combine(remotePath, info.Name), info.FullName, isDel); } } if (isDel) { var items = client.GetListing(remotePath); foreach (var item in items) { if (infos.All(info => info.Name != item.Name)) { if (item.Type == FtpFileSystemObjectType.File) { client.DeleteFile(item.FullName); } else if (item.Type == FtpFileSystemObjectType.Directory) { client.DeleteDirectory(item.FullName); } Logger.Info($"DeletedFtp==>{item.FullName}"); } } } }
private void OnFileChanged(object sender, EventArgs e) { var _e = (FileSystemEventArgs)e; //check if directory is excluded foreach (var ignored_dir in _excludedDirectories) { if (_e.FullPath.StartsWith(ignored_dir)) { return; } } string remotePath; string remoteFullPath; FtpOperationEventArgs ftpOperationArgs; switch (_e.ChangeType.ToString()) { case "Deleted": remotePath = _e.FullPath.Substring(_settings.LocalPath.Length); remotePath = remotePath.Remove(remotePath.Length - _e.Name.Length, _e.Name.Length); remotePath = remotePath.Replace("\\", "/"); remoteFullPath = _settings.RemotePath + remotePath + _e.Name; _ftpClient.DeleteFile(remoteFullPath); ftpOperationArgs = new FtpOperationEventArgs(); ftpOperationArgs.Operation = "deleted"; ftpOperationArgs.RemoteFullPath = remoteFullPath; RaiseOnFtpOperationFinishedEvent(ftpOperationArgs); break; case "Created": case "Changed": remotePath = _e.FullPath.Substring(_settings.LocalPath.Length); remotePath = remotePath.Remove(remotePath.Length - _e.Name.Length, _e.Name.Length); remotePath = remotePath.Replace("\\", "/"); remoteFullPath = _settings.RemotePath + remotePath + _e.Name; var success = _ftpClient.UploadFile(_e.FullPath, remoteFullPath); if (success) { ftpOperationArgs = new FtpOperationEventArgs(); ftpOperationArgs.Operation = "uploaded"; ftpOperationArgs.RemoteFullPath = remoteFullPath; RaiseOnFtpOperationFinishedEvent(ftpOperationArgs); } else { ftpOperationArgs = new FtpOperationEventArgs(); ftpOperationArgs.Operation = "failed to upload"; ftpOperationArgs.RemoteFullPath = remoteFullPath; RaiseOnFtpOperationFinishedEvent(ftpOperationArgs); } break; default: break; } }
private void UploadFile(string FilePath, string UploadPath) { IFtpClient pFtpClient = HR.Utility.SysDBConfig.GetInstance().GetFtpClient("CHXQ"); pFtpClient.Connect(); string Dir = System.IO.Path.GetDirectoryName(UploadPath); if (!pFtpClient.DirectoryExists(Dir)) { pFtpClient.CreateDirectory(Dir); } if (pFtpClient.Exists(UploadPath)) { pFtpClient.DeleteFile(UploadPath); } pFtpClient.UploadFile(FilePath, UploadPath); pFtpClient.Close(); }
public void TestMethod_DeleteFile() { string file = @"r5.jpg"; bool IsDelete = false; string[] fileList = proxyftpClient.GetDirectoryList(); foreach (string fileInfo in fileList) { if (Path.GetFileName(fileInfo) == file) { proxyftpClient.DeleteFile(file, false); IsDelete = true; break; } } if (IsDelete) { string[] refreshList = proxyftpClient.GetDirectoryList(); bool hasFile = false; foreach (string fileInfo in refreshList) { if (Path.GetFileName(fileInfo) == file) { hasFile = true; break; } } if (hasFile) { log2.Debug("檔案刪除失敗:" + file); } else { log2.Debug("檔案刪除成功:" + file); } Assert.IsFalse(hasFile); } else { log2.Debug("遠端檔案:" + file + "不存在,無法刪除"); Assert.IsTrue(IsDelete); } }
public override void Delete() { IFtpClient pFtpClient = SysDBConfig.GetInstance().GetFtpClient("CHXQ"); pFtpClient.Connect(); try { pFtpClient.DeleteFile(this.Path); string sql = string.Format("delete from up_files where ID={0}", this.m_ID); IDataBase OrclDataBase = HR.Utility.SysDBConfig.GetInstance().GetOleDataBase("OrclConn"); OrclDataBase.OpenConnection(); OrclDataBase.ExecuteNonQuery(sql); OrclDataBase.CloseConnection(); } catch (Exception ex) { throw ex; } finally { pFtpClient.Close(); } }