InkSoft.SmbAbstraction
2.0.0
dotnet add package InkSoft.SmbAbstraction --version 2.0.0
NuGet\Install-Package InkSoft.SmbAbstraction -Version 2.0.0
<PackageReference Include="InkSoft.SmbAbstraction" Version="2.0.0" />
paket add InkSoft.SmbAbstraction --version 2.0.0
#r "nuget: InkSoft.SmbAbstraction, 2.0.0"
// Install InkSoft.SmbAbstraction as a Cake Addin #addin nuget:?package=InkSoft.SmbAbstraction&version=2.0.0 // Install InkSoft.SmbAbstraction as a Cake Tool #tool nuget:?package=InkSoft.SmbAbstraction&version=2.0.0
SmbAbstraction
This project is a fork of https://github.com/jordanlytle/SmbAbstraction with a few key differences: 1) It uses the original SMBLibrary as opposed to SMBLibraryLite, 2) It conforms to newer versions of https://github.com/TestableIO/System.IO.Abstractions interfaces, and 3) it also targets .Net Standard 2.0. This library implements the System.IO.Abstractions interfaces for interacting with the filesystem and adds support for interacting with UNC or SMB paths. The intent is to provide an intuitive way to operate against SMB/UNC shares along with being able to operate on UNC shares from Linux/OSX or even Windows clients outside of AD without having to use 'PInvoke' to wrap file share access. This project isn't popular (yet?) and is not guaranteed to work for your specific application.
Usage
Examples
Example projects are available to view in InkSoft.SmbAbstraction.Examples
Basic (Program.cs top level statement file)
using InkSoft.SmbAbstraction;
using System.IO.Abstractions;
const string c_domain = "domain";
const string c_username = "username";
const string c_password = "password";
const string? c_sharePath = @"\\host\ShareName"; // e.g. \\host\ShareName or smb://host/sharename
ISmbCredentialProvider credentialProvider = new SmbCredentialProvider();
ISmbClientFactory clientFactory = new Smb2ClientFactory();
IFileSystem fileSystem = new SmbFileSystem(clientFactory, credentialProvider, null, null);
string path = fileSystem.Path.Combine(c_sharePath, "test.txt");
// SMBCredential will parse the share path from path
using var credential = SmbCredential.AddToProvider(c_domain, c_username, c_password, c_sharePath, credentialProvider);
// FileInfo
var fileInfo = fileSystem.FileInfo.New(path);
// DirectoryInfo
var directoryInfo = fileSystem.DirectoryInfo.New(path);
// Stream
using (var stream = fileSystem.File.Open(path, System.IO.FileMode.Open))
{
// Do stuff...
}
Dependency Injection
Registering Services
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
.ConfigureLogging(logging => { logging.AddConsole(); logging.AddDebug(); })
.ConfigureServices((hostContext, services) => services
.AddSingleton<ISmbCredentialProvider>(p => new SmbCredentialProvider())
// If you don't want to use SmbFileSystem as the default IFileSystem, you can use something like following line instead for specifically requesting it via [FromKeyedServices(nameof(SmbFileSystem))]
// without using the SmbFileSystem type. This makes it such that you can unit-test code with MockFileSystem by registering MockFileSystem to the keyed service instead of an actual SmbFileSystem.
.AddKeyedSingleton<IFileSystem>(nameof(SmbFileSystem), (p, _) => new SmbFileSystem(new Smb2ClientFactory(), p.GetRequiredService<ISmbCredentialProvider>(), null, p.GetRequiredService<ILoggerFactory>()))
// Otherwise, this makes SmbFileSystem the default file system:
.AddSingleton<IFileSystem>(p => new SmbFileSystem(
new Smb2ClientFactory(),
p.GetRequiredService<ISmbCredentialProvider>(),
null,
p.GetRequiredService<ILoggerFactory>()
)));
}
Making calls
using InkSoft.SmbAbstraction;
using Microsoft.Extensions.DependencyInjection;
using System.IO.Abstractions;
using System.Net;
public class NetworkShareService(
[FromKeyedServices(nameof(SmbFileSystem))] IFileSystem fileSystem,
ISmbCredentialProvider credentialProvider
){
private const string c_sharePath = @"\\host\ShareName"; // e.g. \\host\ShareName or smb://host/sharename
private string Path => fileSystem.Path.Combine(c_sharePath, "test.txt");
/// <summary>
/// With IDisposable
/// </summary>
public void FileOpsInContext()
{
// SMBCredential will parse the share path from path. These credentials are removed from the cache when the using block is exited.
using var credential = SmbCredential.AddToProvider("domain", "username", "password", c_sharePath, credentialProvider);
// FileInfo
var fileInfo = fileSystem.FileInfo.New(Path);
// DirectoryInfo
var directoryInfo = fileSystem.DirectoryInfo.New(c_sharePath);
// Stream
using (var stream = fileSystem.File.Open(Path, System.IO.FileMode.Open))
{
// Do stuff...
}
}
/// <summary>
/// With Stored Credentials
/// </summary>
public void StoreCredentialsForShare(NetworkCredential credential)
{
string? domain = credential.Domain;
string? username = credential.UserName;
string? password = credential.Password;
// You can add/cache credentials for a share by using the credentials outside a using block, perhaps here, at app startup, or elsewhere, by specifying false for the last param.
SmbCredential.AddToProvider(domain, username, password, c_sharePath, credentialProvider, false);
}
public void UseStoredCredentialsForFileOp()
{
// FileInfo
var fileInfo = fileSystem.FileInfo.New(Path);
// DirectoryInfo
var directoryInfo = fileSystem.DirectoryInfo.New(c_sharePath);
// Stream
using (var stream = fileSystem.File.Open(Path, System.IO.FileMode.Open))
{
// Do stuff...
}
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- IndexRange (>= 1.0.3)
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.1)
- SMBLibrary (>= 1.5.3.5)
- System.IO.Abstractions (>= 21.0.29)
-
.NETStandard 2.1
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.1)
- SMBLibrary (>= 1.5.3.5)
- System.IO.Abstractions (>= 21.0.29)
-
net8.0
- Microsoft.Extensions.Logging.Abstractions (>= 8.0.1)
- SMBLibrary (>= 1.5.3.5)
- System.IO.Abstractions (>= 21.0.29)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
2.0.0 | 492 | 9/19/2024 |
Initial fork from original SmbAbstraction project. Updated framework and reference library support. Many breaking changes from original project: Case changes of class names, e.g. SMBFileSystem is now SmbFileSystem; Parameter order / method signature changes for main classes.