ComponentPro UltimateFtp

      Connection settings, connection info and connection state

      Language Filter: AllSend comments on this topic to ComponentPro

      Set connection options

      The Ftp class has several properties allowing you to set proxies, transfer mode and transfer type, Encodingcompression modeEnabledFeatures, and more.

      // Create a new instance of the Ftp class. 
      using (Ftp client = new Ftp())
      {
          // Connect to the server. 
          client.Connect(serverName);
      
          client.Authenticate(userName, password);
      
          client.Encoding = System.Text.Encoding.UTF8;
          client.TransferMode = FtpTransferMode.Zlib;
      
          // ... 
          // Transfer files 
          // ...
      }
      

      Get connection info

      Some of the useful information about the current FTP session such as server/user name and system name are available when the connection is established or the user is authenticated.

      // Create a new instance of the Ftp class. 
      using (Ftp client = new Ftp())
      {
          // Connect to the server. 
          client.Connect(serverName);
      
          // Get connection information 
          Console.WriteLine("Server: {0}:{1}, System Name: {2}",
                            client.ServerName, client.ServerPort, client.GetSystemName());
      
          // Authenticate the user 
          client.Authenticate(userName, password);
      
          Console.WriteLine("Logged in as: {0}", client.UserName);
      }
      

      Check connection state

      To determine if a session is still alive, use GetConnectionState method. The method sends a "ping" command to the server to ensure the connection is still up and running.

      // Check session state 
      using (Ftp client = new Ftp())
      {
          // Connect to the server. 
          client.Connect(serverName);
      
          // Authenticate the user 
          client.Authenticate(userName, password);
      
          bool connected;
          int nativeErrorCode;
          // Get connection state. 
          client.GetConnectionState(out connected, out nativeErrorCode);
      
          Console.WriteLine(connected ? "Connection is still alive" : "Connection closed. Error code: " + nativeErrorCode);
      }