title: FTP Download File
body: <p>I am trying to file download from remote machine with FTP in c# but it doesn't work. when i came GetResponse Method with f10, threw the exception. its says:"unable to connect to remote server", it looks like about connection problem but its not.</p>
  
  <p>when i create request in the code ContentType and PreAuthenticate is threw an exception and said:"System.NotSupportedException"</p>
  
  <p>How can i fix this? Any ideas?</p>
  
  <p>Here's the Connection Info for FTP.
  </p>
  
  <pre class="lang-cs prettyprint-override"><code>private static string host = @"ftp://XXX.XX.XXX.XX/";
  private static string user = "XXXXXXXX";
  private static string pass = "XXXXXXXX";
  private static string localfile = @"E:/Files/Attachment";
  private static string remoteFile = @"D:/log/stdlog.6.txt";
  </code></pre>
  
  <p>And then FTP class </p>
  
  <pre class="lang-cs prettyprint-override"><code>public class FtpServer
  {
      private string host = null;
      private string user = null;
      private string pass = null;
      private FtpWebRequest ftpRequest = null;
      private FtpWebResponse ftpResponse = null;
      private Stream ftpStream = null;
      private int bufferSize = 2048;
  
      /* Construct Object */
      public FtpServer(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; }
      public  void Download(string remoteFile, string localFile)
      {
  
          try
          {
  
              ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host+remoteFile);
  
  
              ftpRequest.Credentials = new NetworkCredential(user,pass);
  
              ftpRequest.UseBinary = true;
              ftpRequest.UsePassive = true;
              ftpRequest.KeepAlive = true;
  
              ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
  
              ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  
              ftpStream = ftpResponse.GetResponseStream();
  
              FileStream localFileStream = new FileStream(localFile, FileMode.Create);
  
              byte[] byteBuffer = new byte[bufferSize];
              int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
  
              try
              {
                  while (bytesRead &gt; 0)
                  {
                      localFileStream.Write(byteBuffer, 0, bytesRead);
                      bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                  }
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.ToString());
              }
  
              localFileStream.Close();
              ftpStream.Close();
              ftpResponse.Close();
              ftpRequest = null;
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.ToString());
          }
          return;
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/RzYQB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RzYQB.png" alt="enter image description here"></a></p>