🔹 A .NET 8 library for managing files on a Samba (SMB) server with Dependency Injection support.
SambaFileManager is a lightweight .NET 8 library that simplifies reading, writing, and deleting files on Samba (SMB) servers. It provides an easy-to-use API that integrates seamlessly with Dependency Injection (DI).
✔️ Supports reading, writing, and deleting files over an SMB network.
✔️ Integrates with .NET Dependency Injection for easy use in ASP.NET and Console Apps.
✔️ Provides robust error handling and exception management.
✔️ Uses SMBLibrary under the hood for stable and secure SMB communication.
Install the package via NuGet:
dotnet add package SambaFileManager
Or manually add it to your .csproj
:
<ItemGroup>
<PackageReference Include="SambaFileManager" Version="1.0.0" />
</ItemGroup>
using Microsoft.Extensions.DependencyInjection;
using System;
using SambaFileManager.Interfaces;
using SambaFileManager.Models;
using SambaFileManager.Extensions;
class Program
{
static void Main()
{
// Setup DI container
var sambaSettings = new SambaSettingsBuilder()
.SetServer("192.168.1.100")
.SetShare("storage")
.SetUsername("smbuser")
.SetPassword("smbpassword")
.Build();
var serviceProvider = new ServiceCollection()
.AddSambaFileManagerServices(sambaSettings)
.BuildServiceProvider();
// Resolve the Samba file service
var sambaFileService = serviceProvider.GetRequiredService<ISambaFileService>();
// Define file path
string filePath = "test.txt";
string fileContent = "Hello, Samba File System!";
try
{
// Write a file
Console.WriteLine("Writing file...");
sambaFileService.WriteFile(filePath, fileContent);
Console.WriteLine("File written successfully.");
// Read the file
Console.WriteLine("Reading file...");
string content = sambaFileService.ReadFile(filePath);
Console.WriteLine($"File content: {content}");
// Delete the file
Console.WriteLine("Deleting file...");
sambaFileService.DeleteFile(filePath);
Console.WriteLine("File deleted successfully.");
}
catch (Exception ex)
{
Console.WriteLine($"Error: {ex.Message}");
}
}
}
Writing file...
File written successfully.
Reading file...
File content: Hello, Samba File System!
Deleting file...
File deleted successfully.
var sambaSettings = builder.Configuration.GetSection("Samba").Get<SambaSettings>();
builder.Services.AddSambaFileManagerServices(sambaSettings);
[ApiController]
[Route("api/files")]
public class FileController : ControllerBase
{
private readonly ISambaFileService _sambaFileService;
public FileController(ISambaFileService sambaFileService)
{
_sambaFileService = sambaFileService;
}
[HttpGet("read")]
public IActionResult ReadFile(string filePath)
{
var content = _sambaFileService.ReadFile(filePath);
return Ok(content);
}
}
You can configure your SMB settings in appsettings.json
:
{
"Samba": {
"Server": "192.168.1.100",
"Share": "SharedFolder",
"Username": "smbuser",
"Password": "smbpassword",
"Domain": ""
}
}
This project is licensed under the MIT License. See the LICENSE file for details.
🎯 Found a bug or have an idea for improvement?
Feel free to open an issue or submit a pull request!
🔗 GitHub Issues
If you find this package useful, give it a star ⭐ on GitHub and share it with others! 🚀