/// <summary> /// Соединение /// </summary> public bool Connect() { if (string.IsNullOrEmpty(ftpClient.Host)) { throw new Exception("Не задан хост."); } for (int attempt = 1; attempt <= attempts; attempt++) { try { ftpClient.Connect(); break; } catch (Exception ex) { if (attempt == attempts) { throw new Exception("Ошибка соединения с ftp.", ex); } Thread.Sleep(attemptsTimeout); } } return(ftpClient.IsConnected); }
private void CreateSubDir_Click(object sender, EventArgs e) { TreeNode pNode = this.SelectedNode; int ID = this.GetNodeID(pNode); string ParentPath = this.GetFtpPathByNode(pNode); string sql = "select SEQ_UP_DIRS.nextval from dual"; IDataBase pDataBase = SysDBConfig.GetInstance().GetOleDataBase("OrclConn"); pDataBase.OpenConnection(); string DirID = pDataBase.ExecuteScalar(sql).ToString(); string Path = string.Format("{0}/{1}", this.GetFtpPathByNode(pNode), DirID); sql = string.Format("insert into up_dirs values({0},'新建目录',{1},'{2}\\{0}',1)", DirID, ID, ParentPath); pDataBase.ExecuteNonQuery(sql); pDataBase.CloseConnection(); this.RefreshSubTreeView(pNode); pNode.ExpandAll(); TreeNode NewNode = this.FindNodeByID(int.Parse(DirID)); IFtpClient pFtpClient = SysDBConfig.GetInstance().GetFtpClient("CHXQ"); pFtpClient.Connect(); string FtpPath = GetFtpPathByNode(NewNode); pFtpClient.CreateDirectory(FtpPath); pFtpClient.Close(); this.SelectedNode = NewNode; this.LabelEdit = true; NewNode.BeginEdit(); }
private void Delete_Click(object sender, EventArgs e) { if (MessageBox.Show("是否删除该文件夹", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel) { return; } TreeNode pNode = this.SelectedNode; int ID = this.GetNodeID(pNode); IFtpClient pFtpClient = SysDBConfig.GetInstance().GetFtpClient("CHXQ"); pFtpClient.Connect(); pFtpClient.DeleteDirectory(GetFtpPathByNode(pNode)); pFtpClient.Close(); string sql = string.Format("delete from up_dirs where id={0}", ID); IDataBase pDataBase = SysDBConfig.GetInstance().GetOleDataBase("OrclConn"); pDataBase.OpenConnection(); pDataBase.ExecuteNonQuery(sql); pDataBase.CloseConnection(); this.RefreshSubTreeView(pNode.Parent); }
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 BackupDirectory(IFtpClient client, string remotePath, string localPath, bool isDel) { var items = client.GetListing(remotePath); foreach (var item in items) { if (!client.IsConnected) { client.Connect(); } if (item.Type == FtpFileSystemObjectType.File) { var size = client.GetFileSize(item.FullName); var localFile = Path.Combine(localPath, item.Name); if (!File.Exists(localFile) || new FileInfo(localFile).Length != size) { client.DownloadFile(localFile, item.FullName); Logger.Info($"Downloaded==>{item.FullName}"); } } else if (item.Type == FtpFileSystemObjectType.Directory) { if (!Directory.Exists(item.FullName)) { Directory.CreateDirectory(item.FullName); Logger.Info($"CreateDirectory==>{item.FullName}"); } BackupDirectory(client, item.FullName, Path.Combine(localPath, item.Name), isDel); } } if (isDel) { var localFolder = new DirectoryInfo(localPath); var infos = localFolder.GetFileSystemInfos(); foreach (var info in infos) { if (items.All(item => item.Name != info.Name)) { if (info is DirectoryInfo) { (info as DirectoryInfo).Delete(true); } else { info.Delete(); } Logger.Info($"Deleted==>{info.FullName}"); } } } }
public void UploadFiles(string[] FilePaths, TreeNode pNode) { IFtpClient pFtpClient = SysDBConfig.GetInstance().GetFtpClient("CHXQ"); pFtpClient.Connect(); IDataBase pDataBase = SysDBConfig.GetInstance().GetOleDataBase("OrclConn"); pDataBase.OpenConnection(); string DirPath = GetFtpPathByNode(pNode); try { if (!pFtpClient.DirectoryExists(DirPath)) { pFtpClient.CreateDirectory(DirPath); } foreach (string FilePath in FilePaths) { string RemotePath = string.Format("{0}\\{1}", DirPath, System.IO.Path.GetFileName(FilePath)); pFtpClient.UploadFile(FilePath, RemotePath); string ProcedureName = "AddFile"; IDataParameter[] DataParameters = new IDataParameter[] { new OracleParameter() { ParameterName = "V_FileName", OracleType = OracleType.NVarChar, Value = System.IO.Path.GetFileNameWithoutExtension(FilePath) }, new OracleParameter() { ParameterName = "V_Path", OracleType = OracleType.NVarChar, Value = RemotePath }, new OracleParameter() { ParameterName = "V_DirID", OracleType = OracleType.Number, Value = this.GetNodeID(pNode) } }; pDataBase.ExecuteProcedure(ProcedureName, ref DataParameters); } } catch (Exception ex) { throw ex; } finally { pDataBase.CloseConnection(); pFtpClient.Close(); } }
public FtpService(IOptions <FtpConfiguration> options, ILog log) { _log = log; _options = options.Value; _ftpClient = new FtpClient(_options.Host, new NetworkCredential(_options.User, _options.Password)); if (_options.WithSSL) { _ftpClient.EncryptionMode = FtpEncryptionMode.Explicit; _ftpClient.SslProtocols = _options.SslProtocol; } _ftpClient.Connect(); }
private void connectButton_Click(object sender, EventArgs e) { try { _client.Connect(userNameTextBox.Text, passwordTextBox.Text, serverUriTextBox.Text); _client.ListingDirectoryReceived += ClientOnFirstListingDirectoryReceived; topToolStrip.Enabled = false; remoteFilesList.Items.Clear(); remoteFilesList.Enabled = false; _client.GetDirectoryListing(); } catch (Exception exception) { ShowErrorAlert(exception.Message); } }
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 virtual void DownloadFile(string SavePath) { IFtpClient pFtpClient = SysDBConfig.GetInstance().GetFtpClient("CHXQ"); pFtpClient.Connect(); try { pFtpClient.DownloadFile(SavePath, this.Path); } catch (Exception ex) { throw ex; } finally { pFtpClient.Close(); } }
public virtual void DownloadFile(string remotePathAndFile, string localPathAndFile) { _ftpClient.Host = _configuration.FtpHost; _ftpClient.Credentials = new NetworkCredential(_configuration.FtpUsername, _configuration.FtpPassword); try { // begin connecting to the server _ftpClient.Connect(); _ftpClient.DownloadFile(localPathAndFile, remotePathAndFile); } catch (Exception ex) { throw new FtpException(ex.Message); } finally { _ftpClient.Disconnect(); } }
public void DownloadAD(string SavePath) { if (string.IsNullOrEmpty(m_CADPath)) { throw new Exception("图纸未上传"); } IFtpClient pFtpClient = HR.Utility.SysDBConfig.GetInstance().GetFtpClient("CHXQ"); pFtpClient.Connect(); try { pFtpClient.DownloadFile(SavePath, this.m_CADPath); } catch (Exception ex) { throw ex; } finally { pFtpClient.Close(); } }
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(); } }
public void Connect() { DateTime retryMaxTime = DateTime.Now + TimeSpan.FromSeconds(30); _ftpClient = _ftpClientFactory.Create(); while (DateTime.Now < retryMaxTime) { try { _ftpClient.Connect(); break; } catch (FtpCommandException ex) { if (ex.IsTransient()) { Serilog.Log.Warning(ex, "Unable to connect. Retrying"); Thread.Sleep(2000); } else { throw; } } catch (TimeoutException ex) { Serilog.Log.Warning(ex, "Timeout. Unable to connect. Retrying"); Thread.Sleep(2000); } } if (!_ftpClient.IsConnected) { throw new InvalidOperationException("Unable to connect to FTP. Maximum retries exceeded"); } }
public void Run() { _ftpClient.Connect(); _folderMonitor.Watch(); }