I have to upload a file to an ftp server (local host 127.0.0.1) but first I have to make a backup of that file (with a different name from the original) from where I upload it, this second part is what I can't do. Here is what I have so far:
//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();
}
If the problem is simply saving a copy of the source file by appending
_bk
to it, instead of using aStreamReader
I would simply useFile.Copy
to make a copy. In your case, it would be something like this:Note that with the third parameter a
true
, the destination file will be overwritten if it exists. On the other hand, you will need to add the namespaceSystem.IO
. Lastly, this copy should be the first thing you do, so I'd move this to the beginning of yourFtpUploadExeAndBk
.