private void button1_Click_3(object sender, EventArgs e) { using (WinSCP.Session session = new WinSCP.Session()) { fingerprint = session.ScanFingerprint(sessionOptions, "MD5"); sessionOptions.SshHostKeyFingerprint = fingerprint; session.Open(sessionOptions); TransferOptions transferOptions = new TransferOptions(); transferOptions.TransferMode = TransferMode.Binary; TransferOperationResult transferResult; session.RemoveFiles(@"/c:/MalCode/WinForm.exe"); //transferResult.Check(); //foreach (TransferEventArgs transfer in transferResult.Transfers) //{ // System.Console.WriteLine("Upload of {0} succeeded ", transfer.FileName); //} } }
public void DeleteOld(Database db) { if (MySQLBackUpFTP_ADOPSE.Properties.Settings.Default.deleteOld && MySQLBackUpFTP_ADOPSE.Properties.Settings.Default.keepOnFTP > 0 && MySQLBackUpFTP_ADOPSE.Properties.Settings.Default.sendBackup) { deleteAttempts++; try { using (Session session = new Session()) { session.Open(sessionOptions); ArrayList files = new ArrayList(); RemoteDirectoryInfo directory = session.ListDirectory(session.HomePath); Regex regex = new Regex("^DB_"+db.GetAlias()+@"_bu............\.sql$"); for (int i = 0; i < directory.Files.Count; i++) { Match match = regex.Match(directory.Files[i].Name); if (match.Success) files.Add(directory.Files[i].Name); } files.Sort(); for (int i = 0; i < files.Count - MySQLBackUpFTP_ADOPSE.Properties.Settings.Default.keepOnFTP; i++) session.RemoveFiles((string)files[i]); } } catch (Exception) { if (deleteAttempts < 4) DeleteOld(db); else new Log("Error trying to delete backup file(s) from the FTP server."); } } }
public int removeFTPFiles(String HostName, String UserName, String Password, String remoteFilePath) { int result = 0; Session session = null; try { // Setup session options SessionOptions sessionOptions = new SessionOptions { Protocol = Protocol.Ftp, HostName = HostName, UserName = UserName, Password = Password, //HostName = "119.59.73.48", //UserName = "******", //Password = "******", // SshHostKeyFingerprint = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" }; using (session = new Session()) { // Connect session.Open(sessionOptions); // Remove files RemovalOperationResult removalResult = null; removalResult = session.RemoveFiles(remoteFilePath); // Throw on any error removalResult.Check(); // Print results foreach (RemovalEventArgs removal in removalResult.Removals) { Console.WriteLine("Remove of {0} succeeded", removal.FileName); } } result = 0; } catch (Exception e) { Console.WriteLine("Error: {0}", e); result = 1; } finally { if (session != null) { session.Dispose(); } } return result; }