/// <summary> /// Lanza las pruebas sobre el servidor FTP /// </summary> private static void TestFtp(FtpClient.FtpProtocol intMode, string strServer, int intPort, string strUser, string strPassword, bool blnPassive) { using (FtpClient objFtpClient = new FtpClient(intMode, strServer, intPort, new System.Net.NetworkCredential(strUser, strPassword), GetClientParameters(intMode, blnPassive))) { // Log Log(0, string.Format("Pruebas de {0}:{1} - {2} - Pasivo: {3}", strServer, intPort, intMode, blnPassive), false); // Asigna el manejador de eventos objFtpClient.ConnectionInitialized += (objSender, objEventArgs) => { System.Diagnostics.Debug.WriteLine("Conexión inicializada", false); }; objFtpClient.IOError += (objSender, objEventArgs) => { System.Diagnostics.Debug.WriteLine("IOError: " + objEventArgs.ToString(), false); }; objFtpClient.Reply += (objSender, objEventArgs) => { System.Diagnostics.Debug.WriteLine("Reply: " + objEventArgs.Reply.ToString(), false); }; objFtpClient.Request += (objSender, objEventArgs) => { System.Diagnostics.Debug.WriteLine("Request: " + objEventArgs.RequestCommand, false); }; // Conecta el cliente objFtpClient.Connect(); // Crea el directorio /cnstStrPathRemote if (objFtpClient.Commands.MakeDir(cnstStrPathRemote)) { Log(1, "Creado el directorio " + cnstStrPathRemote, false); } // Comprueba los cambios de directorio objFtpClient.Commands.ChangeDir("/"); objFtpClient.Commands.ChangeDir(cnstStrPathRemote); objFtpClient.Commands.ChangeDir("/"); if (objFtpClient.Commands.GetActualPath() != "/") { Log(1, "No se puede pasar al directorio /", true); } else { Log(1, "Pruebas de paso al directorio " + cnstStrPathRemote + " superadas", false); } // Comprueba la creación de directorios if (!objFtpClient.Commands.MakeDirRecursive(cnstStrPathRemote + "/hola/test/pruebas/")) { Log(1, "No se ha podido crear el directorio recursivo", true); } objFtpClient.Commands.ChangeDir(cnstStrPathRemote + "/hola/test/pruebas/"); if (!objFtpClient.Commands.ChangeDir(cnstStrPathRemote + "/hola/test/pruebas/")) { Log(1, "No se ha creado el directorio recursivo", true); } if (objFtpClient.Commands.RemoveDirectory(cnstStrPathRemote + "/hola")) { Log(1, "Se ha borrado un directorio intermedio. No debería pasar", true); } // Borra los directorios creados recursivamente (excepto el DEV) objFtpClient.Commands.RemoveDirectory(cnstStrPathRemote + "/hola/test/pruebas/"); objFtpClient.Commands.RemoveDirectory(cnstStrPathRemote + "/hola/test/"); objFtpClient.Commands.RemoveDirectory(cnstStrPathRemote + "/hola"); if (objFtpClient.Commands.ChangeDir(cnstStrPathRemote + "/hola")) { Log(1, "No debería existir este directorio", true); } // Obtiene el directorio del servidor objFtpClient.Commands.ChangeDir(cnstStrPathRemote); if (!objFtpClient.Commands.GetActualPath().Equals(cnstStrPathRemote, StringComparison.CurrentCultureIgnoreCase)) { Log(1, "No se ha cambiado al directorio " + cnstStrPathRemote, true); } // Borra el directorio de pruebas (esté o no) objFtpClient.Commands.RemoveDirectory(cnstStrPathRemote + "/test"); // Crea un nuevo directorio objFtpClient.Commands.MakeDir(cnstStrPathRemote + "/test"); objFtpClient.Commands.ChangeDir(cnstStrPathRemote + "/test"); if (!objFtpClient.Commands.GetActualPath().Equals(cnstStrPathRemote + "/test", StringComparison.CurrentCultureIgnoreCase)) { Log(1, "No se ha cambiado al directorio dev/test. Es posible que no se haya creado", true); } // Intenta crear de nuevo el mismo directorio pero esta vez sin tener en cuenta las excepciones if (objFtpClient.Commands.MakeDir(cnstStrPathRemote + "/test")) { Log(1, "No debería haber creado correctamente el directorio", true); } // Borra el directorio que acabamos de crear para dejar todo como estaba objFtpClient.Commands.RemoveDirectory(cnstStrPathRemote + "/test"); // Prueba las subidas y descargas Log(1, "Pruebas de subida y descarga", false); TestSaveFiles(objFtpClient, "Test.txt", false); TestSaveFiles(objFtpClient, "Test.txt", true); // Prueba de lista Log(1, "Prueba de comando LIST", false); TestList(objFtpClient.Commands.List(cnstStrPathRemote)); Log(1, "Pureba de comando MLST", false); TestList(objFtpClient.Commands.MList(cnstStrPathRemote)); // Log Log(0, string.Format("Fin de pruebas de {0}:{1} - {2} - Pasivo: {3}", strServer, intPort, intMode, blnPassive), false); Log(0, new string('-', 50), false); Log(0, "", false); } }
/// <summary> /// Obtiene los parámetros del cliente /// </summary> private static Bau.Libraries.LibFtpClient.Parameters.FtpClientParameters GetClientParameters(FtpClient.FtpProtocol intMode, bool blnPassive) { return(new Bau.Libraries.LibFtpClient.Parameters.FtpClientParameters(blnPassive)); }