1

Tengo que subir un fichero a un servidor ftp (local host 127.0.0.1) pero antes tengo que hacer un backup de ese fichero (con nombre diferente del original) desde donde lo subo, esta segunda parte es la que no consigo hacer. Os dejo lo que tengo hasta ahora:

//Upload a file from a X Path to the FTP Server and backup of the file
public static void FtpUploadExeAndBk(string direccionIP, string username, string password)
{
    // Get the object used to communicate with the server
    Uri uri = new Uri(string.Concat("ftp://", direccionIP, "/", "Configurator.exe"));
    FtpWebRequest ftpWebRequest = (FtpWebRequest)WebRequest.Create(uri);
    FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
    request.Method = WebRequestMethods.Ftp.UploadFile;

    // This example assumes the FTP site uses anonymous logon
    request.Credentials = new NetworkCredential(username, password);

    // Copy the contents of the file to the request stream
    StreamReader sourceStream = new StreamReader(@"C:\Projects\Configurator.exe");
    byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
    sourceStream.Close();
    request.ContentLength = fileContents.Length;

    //Hacer el backup y que se quede en la carpeta local (renombrar) ?????
    StreamReader sourceStreamBk = new StreamReader(@"C:\Projects\Configurator.exe");
    sourceStreamBk.ToString=sourceStreamBk.ToString.Concat("_bk");
    //??????

    Stream requestStream = request.GetRequestStream();
    requestStream.Write(fileContents, 0, fileContents.Length);
    requestStream.Close();

    FtpWebResponse response = (FtpWebResponse)request.GetResponse();

    Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);

    response.Close();
}  
0

1 respuesta 1

2

Si el problema es simplemente guardar una copia del archivo origen añadiendo _bk al mismo, en lugar de usar un StreamReader yo utilizaría simplemente File.Copy para hacer una copia. En tu caso, quedaría algo asi:

//Hacer el backup y que se quede en la carpeta local (renombrar) 
File.Copy(@"C:\Projects\Configurator.exe",@"C:\Projects\Configurator.exe_bk",true);
//

Ten en cuenta que con el tercer parámetro a true, se sobreescribirá el archivo destino si existe. Por otro lado, deberás añadir el espacio de nombres System.IO. Por último, esta copia debería ser lo primero que hicieras, así que yo movería esto al comienzo de tu método FtpUploadExeAndBk.

1
  • era lo que necesitaba, gracias!
    – LopezAi
    el 31 oct. 2017 a las 10:00

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.