public Delete ( string path ) : void | ||
path | string | The name of the file or directory to be deleted. Wildcard characters are not supported. |
return | void |
/// <summary> /// Downloads the files. /// </summary> /// <param name="fileList">The file list.</param> /// <param name="failRemoteNotExists">if set to <c>true</c> [fail remote not exists].</param> /// <exception cref="System.Exception"> /// Local File Already Exists. /// </exception> public void DownloadFiles(List<ISFTPFileInfo> fileList) { try { this.Log(String.Format("Connecting to Host: [{0}].", this.hostName), LogLevel.Minimal); using (SftpClient sftp = new SftpClient(this.hostName, this.portNumber, this.userName, this.passWord)) { sftp.Connect(); this.Log(String.Format("Connected to Host: [{0}].", this.hostName), LogLevel.Verbose); // Download each file foreach (SFTPFileInfo filePath in fileList) { if (!sftp.Exists(filePath.RemotePath)) { this.Log(String.Format("Remote Path Does Not Exist: [{0}].", this.hostName), LogLevel.Verbose); if (this.stopOnFailure) throw new Exception(String.Format("Remote Path Does Not Exist: [{0}]", filePath.RemotePath)); else continue; } if (Directory.Exists(filePath.LocalPath)) filePath.LocalPath = Path.Combine(filePath.LocalPath, filePath.RemotePath.Substring(filePath.RemotePath.LastIndexOf("/") + 1)); // Can we overwrite the local file if (!filePath.OverwriteDestination && File.Exists(filePath.LocalPath)) throw new Exception("Local File Already Exists."); this.Log(String.Format("Downloading File: [{0}] -> [{1}].", filePath.RemotePath, filePath.LocalPath), LogLevel.Minimal); using (FileStream fileStream = File.OpenWrite(filePath.LocalPath)) { sftp.DownloadFile(filePath.RemotePath, fileStream); } this.Log(String.Format("File Downloaded: [{0}]", filePath.LocalPath), LogLevel.Verbose); // Remove remote file if (filePath.RemoveSource) { this.Log(String.Format("Removing File: [{0}]", filePath.RemotePath), LogLevel.Minimal); sftp.Delete(filePath.RemotePath); this.Log(String.Format("Removed File: [{0}]", filePath.RemotePath), LogLevel.Verbose); } } } this.Log(String.Format("Disconnected from Host: [{0}].", this.hostName), LogLevel.Minimal); } catch (Exception ex) { this.Log(String.Format("Disconnected from Host: [{0}].", this.hostName), LogLevel.Minimal); this.ThrowException("Unable to Download: ", ex); } }
public override async Task Remove(string cpath, bool isFolder = false) { var caughtException = default(Exception); await Task.Run(() => { try { if (isFolder) { _sftpc.DeleteDirectory(cpath); } else { _sftpc.Delete(cpath); } } catch (Exception ex) { ex.LogException(); caughtException = ex; } }); if (caughtException != default(Exception)) { throw caughtException; } }
public static void DeleteRemoteFile(string host, string file, string username, string password) { using (var sftp = new SftpClient(host, username, password)) { sftp.Connect(); sftp.Delete(file); sftp.Disconnect(); } }
/// <summary> /// Uploads the files. /// </summary> /// <param name="fileList">The file list.</param> /// <exception cref="System.Exception">Remote File Already Exists.</exception> public void UploadFiles(List<ISFTPFileInfo> fileList) { try { this.Log(String.Format("Connecting to Host: [{0}].", this.hostName), LogLevel.Minimal); using (SftpClient sftp = new SftpClient(this.hostName, this.portNumber, this.userName, this.passWord)) { sftp.Connect(); this.Log(String.Format("Connected to Host: [{0}].", this.hostName), LogLevel.Verbose); // Upload each file foreach (SFTPFileInfo sftpFile in fileList) { FileInfo fileInfo = new FileInfo(sftpFile.LocalPath); if (sftpFile.RemotePath.EndsWith("/")) sftpFile.RemotePath = Path.Combine(sftpFile.RemotePath, fileInfo.Name).Replace(@"\", "/"); // if file exists can we overwrite it. if (sftp.Exists(sftpFile.RemotePath)) { if (sftpFile.OverwriteDestination) { this.Log(String.Format("Removing File: [{0}].", sftpFile.RemotePath), LogLevel.Verbose); sftp.Delete(sftpFile.RemotePath); this.Log(String.Format("Removed File: [{0}].", sftpFile.RemotePath), LogLevel.Verbose); } else { if (this.stopOnFailure) throw new Exception("Remote File Already Exists."); } } using (FileStream file = File.OpenRead(sftpFile.LocalPath)) { this.Log(String.Format("Uploading File: [{0}] -> [{1}].", fileInfo.FullName, sftpFile.RemotePath), LogLevel.Minimal); sftp.UploadFile(file, sftpFile.RemotePath); this.Log(String.Format("Uploaded File: [{0}] -> [{1}].", fileInfo.FullName, sftpFile.RemotePath), LogLevel.Verbose); } } } this.Log(String.Format("Disconnected from Host: [{0}].", this.hostName), LogLevel.Minimal); } catch (Exception ex) { this.Log(String.Format("Disconnected from Host: [{0}].", this.hostName), LogLevel.Minimal); this.ThrowException("Unable to Upload: ", ex); } }