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:
- Using Directives: The code begins with necessary
usingdirectives. TheRenci.SshNetnamespace is essential for SFTP operations. - Class Definition: The
SftpClientExampleclass encapsulates the functionality of the SFTP client. - Connection Parameters: The class contains private fields for the server’s host, username, password, and port. These should be replaced with actual credentials.
- UploadFile Method:
- This method takes two parameters: the local file path and the remote file path.
- It creates an instance of
SftpClientand attempts to connect to the server. - Upon successful connection, it opens a
FileStreamfor the local file and uploads it to the specified remote path using theUploadFilemethod. - 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.
- Main Method: The entry point of the application where an instance of
SftpClientExampleis created. It specifies the local and remote file paths and calls theUploadFilemethod 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… 🙂
I’ve been designing web applications—on and off—since 2001, back when animated GIFs were all the rage and ‘responsive design’ meant answering your client’s emails. Over the past 14 years, I’ve kept pace with the ever-evolving trends in PHP development, successfully delivering a variety of projects that made my clients happy (and kept me caffeinated).
This website serves as my soapbox—a place to share the insights I’ve picked up along the way with anyone curious enough to dive in. Welcome aboard!
Need some custom work done? Or, just want to reach out? Email: dan@danoriordan.com