problem when copying file from local and moving it to ftp server c #

0

I have to copy an .exe from a local folder and move it to an ftp server, in doing so, the .exe becomes a larger file and does not 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();  
    }  
    
asked by LopezAi 31.10.2017 в 16:47
source

1 answer

1

You are probably corrupting the data with the following statements, where you try to read the executable as if it were UTF-8 data:

StreamReader sourceStream = new StreamReader(@"C:\Projects\Configurator.exe");
byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

Rather, it reads the data in bytes directly to avoid problems:

byte[] fileContents = File.ReadAllBytes(@"C:\Projects\Configurator.exe");
    
answered by 31.10.2017 / 16:51
source