Creating a project
mkdir ftpserver
cd ftpserver
dotnet new console
Adding the NuGet packages
# For dependency injection support (required)
dotnet add package Microsoft.Extensions.DependencyInjection
# For the main FTP server
dotnet add package FubarDev.FtpServer
# For the System.IO-based file system access
dotnet add package FubarDev.FtpServer.FileSystem.DotNet
Using the FTP server
Change your Program.cs
to the following code:
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using FubarDev.FtpServer;
using FubarDev.FtpServer.FileSystem.DotNet;
using Microsoft.Extensions.DependencyInjection;
namespace QuickStart
{
class Program
{
static async Task Main(string[] args)
{
// Setup dependency injection
var services = new ServiceCollection();
// use %TEMP%/TestFtpServer as root folder
services.Configure<DotNetFileSystemOptions>(opt => opt
.RootPath = Path.Combine(Path.GetTempPath(), "TestFtpServer"));
// Add FTP server services
// DotNetFileSystemProvider = Use the .NET file system functionality
// AnonymousMembershipProvider = allow only anonymous logins
services.AddFtpServer(builder => builder
.UseDotNetFileSystem() // Use the .NET file system functionality
.EnableAnonymousAuthentication()); // allow anonymous logins
// Configure the FTP server
services.Configure<FtpServerOptions>(opt => opt.ServerAddress = "127.0.0.1");
// Build the service provider
using (var serviceProvider = services.BuildServiceProvider())
{
// Initialize the FTP server
var ftpServerHost = serviceProvider.GetRequiredService<IFtpServerHost>();
// Start the FTP server
await ftpServerHost.StartAsync();
Console.WriteLine("Press ENTER/RETURN to close the test application.");
Console.ReadLine();
// Stop the FTP server
await ftpServerHost.StopAsync();
}
}
}
}
Starting the FTP server
dotnet run
Now your FTP server should be accessible at 127.0.0.1:21
.