Upload file to ftp server and backup

1

I have to upload a file to an ftp server (local host 127.0.0.1) but before I have to make a backup of that file (with a name different from the original) from where I upload it, this second part is what I can not do . I leave what I have until now:

//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();
}  
    
asked by LopezAi 31.10.2017 в 10:34
source

1 answer

2

If the problem is simply saving a copy of the source file by adding _bk to it, instead of using a StreamReader I would simply use File.Copy to make a copy. In your case, it would be something like this:

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

Note that with the third parameter a true , the destination file will be overwritten if it exists. On the other hand, you must add the namespace System.IO . Finally, this copy should be the first thing you did, so I would move this to the beginning of your FtpUploadExeAndBk method.

    
answered by 31.10.2017 / 10:47
source