What is the error in this loop?

0

I want to download an .exe file from my FTP server, but it has a part in the code that is giving me trouble.

Error: Invalid expression term 'while' (CS1525)

Can anyone tell me what's wrong?

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(serverPath));
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.Credentials = new NetworkCredential(userName, userPassword);
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            Stream responseStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(responseStream);
            FileStream fs = File.Create(destinationFile + @"\app.exe");
            byte[] buffer = new byte[32 * 1024];
            int read;
            sr.Read(while ((read = sr.Read(buffer,0,buffer.Length)) > 0)
            {
                fs.Write(buffer, 0, read);
            }
    
asked by anonymous 10.09.2017 / 17:15

2 answers

3

The error is that a while can not be used as a parameter. I think you want to do the other way around:

while (sr.Read(buffer, 0, buffer.Length) > 0)
{
    fs.Write(buffer, 0, read);
}

Your code is a bit confusing. It does not seem to do what you want it to do.

    
10.09.2017 / 17:20
0
  FtpWebRequest request = (FtpWebRequest)WebRequest.Create(new Uri(serverPath));
        request.Method = WebRequestMethods.Ftp.DownloadFile;
        request.Credentials = new NetworkCredential(userName, userPassword);
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        Stream responseStream = response.GetResponseStream();
        StreamReader sr = new StreamReader(responseStream);
        FileStream fs = File.Create(destinationFile + @"\app.exe");
        byte[] buffer = new byte[32 * 1024];
        int read;
        while ((read = sr.Read(buffer,0,buffer.Length) > 0)
        {
            fs.Write(buffer, 0, read);
        }
    
10.09.2017 / 17:20