public void DeleteFile(string sName, bool fIgnoreNotExist) { string fullFilePath = Path.Combine(this.currentRemotePath, sName); if (!this.FileExists(sName)) { if (fIgnoreNotExist) { return; } this.ErrorMsg = "刪除檔案失敗: 檔案名稱:" + fullFilePath + "不存在!"; throw new Exception("Delete " + fullFilePath + " failed: not exists"); } else { try { RemovalOperationResult removeResult = this.session.RemoveFiles(fullFilePath); removeResult.Check(); } catch (Exception ex) { this.ErrorMsg = "刪除檔案失敗: 檔案名稱:" + fullFilePath + "\r錯誤訊息:" + ex.StackTrace; throw new Exception("Delete " + fullFilePath + " failed: " + ex.Message); } } }
public static int Main() { SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Sftp, HostName = EdiConfiguration.FtpIpAddressOrHostName, UserName = EdiConfiguration.FtpUserName, Password = EdiConfiguration.FtpPassword, SshHostKeyFingerprint = EdiConfiguration.SshHostKeyFingerprint, PortNumber = EdiConfiguration.FtpPortNumber }; using (Session session = new Session()) { session.Open(sessionOptions); TransferOptions transferOptions = new TransferOptions(); transferOptions.TransferMode = TransferMode.Binary; transferOptions.ResumeSupport.State = TransferResumeSupportState.Off; // Download the files in the OUT directory. TransferOperationResult transferOperationResult = session.GetFiles(EdiConfiguration.FtpDirectory, EdiConfiguration.IncommingFilePath, false, transferOptions); // Check and throw if there are any errors with the transfer operation. transferOperationResult.Check(); // Remove files that have been downloaded. foreach (TransferEventArgs transfer in transferOperationResult.Transfers) { RemovalOperationResult removalResult = session.RemoveFiles(session.EscapeFileMask(transfer.FileName)); if (!removalResult.IsSuccess) { eventLogUtility.WriteToEventLog("There was an error removing the file: " + transfer.FileName + " from " + sessionOptions.HostName + ".", EventLogEntryType.Error); } } } }
private void Delete(string pathToDelete, Session myConnection) { RemovalOperationResult result = myConnection.RemoveFiles(pathToDelete); if (result.IsSuccess == false) { IspisPoruka("(BRISANJE) " + pathToDelete + " nije obrisan!", "Pogreska"); } else { IspisPoruka("(BRISANJE) " + pathToDelete + " je obrisan.", "OK"); } }
/// <summary> /// Recursive deleting of files and folder. /// </summary> /// <param name="session"></param> /// <param name="verbose"></param> /// <param name="currentDirectory"></param> private static void DeleteDirectoryRecursive(Session session, bool verbose, string currentDirectory) { RemoteDirectoryInfo directory = session.ListDirectory(currentDirectory); foreach (RemoteFileInfo fileInfo in directory.Files) { if (fileInfo.IsDirectory) { //Skip .. directory if (fileInfo.Name != "..") { DeleteDirectoryRecursive(session, verbose, currentDirectory + "/" + fileInfo.Name); } } else { if (verbose) { Console.Write("Deleting {0}/{1}...", currentDirectory, fileInfo.Name); } RemovalOperationResult r = session.RemoveFiles(session.EscapeFileMask(currentDirectory + "/" + fileInfo.Name)); if (verbose) { Console.WriteLine(r.IsSuccess ? "ok." : "fail."); } } } //After deleting all files recursive, delete the directory itself. if (verbose) { Console.Write("Deleting {0}/...", currentDirectory); } RemovalOperationResult dr = session.RemoveFiles(session.EscapeFileMask(currentDirectory)); if (verbose) { Console.WriteLine(dr.IsSuccess ? "ok." : "fail."); } }