Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
Hi everyone!

I need some verification on ftp connections. Below is what I have so far to keep the connection alive and open:
FTPWebRequest request = (FtpWebRequest)WebRequest.Create("url");
request.KeepAlive = true;
request.ReadWriteTimeout = 600000;

My goal is to be able to keep the connection open indefinitely until all of the files have been uploaded via FTP successfully. Do I have the code correct? If my goal isn't possible, is the only way to accomplish this to raise the readwritetimeout time very high?

Thanks everyone!
Posted 21-Jun-13 10:47am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1


FTPWebRequest.KeepAlive property will let you specify a boolean value whether the control connection to the FTP server is closed after the request completes.

FTPWebRequest.ReadWriteTimeout property will let you specify the number of milliseconds before the reading or writing times out. The default value is 300,000 milliseconds (5 minutes).
If your file are very big and/or you have a slow internet connection this can be a problem.

You can try upload the files asynchronously.

Please read the class documentation here.

Sample code to upload a file to a ftp server:
FtpWebRequest ftpRequest;
FtpWebResponse ftpResponse;
    //Settings required to establish a connection with the server
    this.ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://ServerIP/FileName"));
    this.ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
    this.ftpRequest.Proxy = null;
    this.ftpRequest.UseBinary = true;
    this.ftpRequest.Credentials = new NetworkCredential("UserName", "Password");
    //Selection of file to be uploaded
    FileInfo ff = new FileInfo("File Local Path With File Name");//e.g.: c:\\Test.txt
    byte[] fileContents = new byte[ff.Length];
    //will destroy the object immediately after being used
    using (FileStream fr = ff.OpenRead())
        fr.Read(fileContents, 0, Convert.ToInt32(ff.Length));
    using (Stream writer = ftpRequest.GetRequestStream())
        writer.Write(fileContents, 0, fileContents.Length);
    //Gets the FtpWebResponse of the uploading operation
    this.ftpResponse = (FtpWebResponse)this.ftpRequest.GetResponse(); 
    Response.Write(this.ftpResponse.StatusDescription); //Display response
catch (WebException webex)
    this.Message = webex.ToString();

joshrduncan2012 at 21-Jun-13 16:13pm
Thanks JAFC!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 395
2 Maciej Los 285
3 ProgramFOX 265
4 CHill60 200

Advertise | Privacy | Mobile
Web01 | 2.8.150331.1 | Last Updated 21 Jun 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100