I need to copy an .exe from a local folder and move it to an ftp server, when I do this the .exe becomes a larger file and won't let me run it. This is the code I have:
public static void FtpBkAndUploadNewVersion(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;
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();
}
You are probably corrupting the data with the following statements, where you try to read the executable as if it were UTF-8 data:
Rather, read the data in bytes directly to avoid problems: