Secure FTP Client in C#

In this post, we will create a secure FTP client using C# that allows for the transfer of files to a Unix web server. The client will utilize the SFTP (SSH File Transfer Protocol) to ensure secure file transfers. We will leverage the SSH.NET library, which provides a robust implementation for SFTP operations.

using System;
using System.IO;
using Renci.SshNet;

class SftpClientExample
{
    private string host = "your_unix_server.com";
    private string username = "your_username";
    private string password = "your_password";
    private int port = 22; // Default SFTP port

    public void UploadFile(string localFilePath, string remoteFilePath)
    {
        using (var sftp = new SftpClient(host, port, username, password))
        {
            try
            {
                sftp.Connect();
                Console.WriteLine("Connected to the server.");

                using (var fileStream = new FileStream(localFilePath, FileMode.Open))
                {
                    sftp.UploadFile(fileStream, remoteFilePath);
                    Console.WriteLine($"File {localFilePath} uploaded to {remoteFilePath}.");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred: {ex.Message}");
            }
            finally
            {
                if (sftp.IsConnected)
                {
                    sftp.Disconnect();
                    Console.WriteLine("Disconnected from the server.");
                }
            }
        }
    }

    static void Main(string[] args)
    {
        var client = new SftpClientExample();
        string localFilePath = @"C:\path\to\your\file.txt"; // Local file path
        string remoteFilePath = "/path/on/server/file.txt"; // Remote file path

        client.UploadFile(localFilePath, remoteFilePath);
    }
}

The provided code defines a simple SFTP client in C# that connects to a Unix web server and uploads a specified file. Here’s a breakdown of the key components:

  1. Using Directives: The code begins with necessary using directives. The Renci.SshNet namespace is essential for SFTP operations.
  2. Class Definition: The SftpClientExample class encapsulates the functionality of the SFTP client.
  3. Connection Parameters: The class contains private fields for the server’s host, username, password, and port. These should be replaced with actual credentials.
  4. UploadFile Method:
    • This method takes two parameters: the local file path and the remote file path.
    • It creates an instance of SftpClient and attempts to connect to the server.
    • Upon successful connection, it opens a FileStream for the local file and uploads it to the specified remote path using the UploadFile method.
    • It includes error handling to catch and display any exceptions that may occur during the process.
    • Finally, it ensures that the connection is closed after the operation.
  5. Main Method: The entry point of the application where an instance of SftpClientExample is created. It specifies the local and remote file paths and calls the UploadFile method to perform the upload.

This code provides a straightforward implementation of a secure FTP client in C#, making it easy to transfer files to a Unix web server securely.

Happy coding… 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *