What’s wrong with this loop?

Asked

Viewed 124 times

0

I am wanting to download a.exe file from my FTP server, but there is a part of the code that is giving me trouble.

Error: Invalid expression term while (CS1525)

someone can 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);
            }

2 answers

3


The mistake is that a while cannot be used as a parameter. I believe you want to do it backwards:

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

Your code is a little confused. It doesn’t seem to do what you want it to do.

  • Your answer solved the while problem, but since you said my code doesn’t make sense '-' it pointed out other errors, do you know how I can download a.exe file from my FTP server? I am hours looking for a solution and I can’t find.

  • @Pauloaleixo I don’t know if this is exactly what you want, but in the old days I used the following code to check software updates and download them.

  • I think not, I’ll give solved because solved the problem with the while.

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);
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.