Giter Site home page Giter Site logo

vitskr / smbabstraction Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jordanlytle/smbabstraction

0.0 0.0 0.0 309 KB

Implements the System.IO.Abstractions interfaces for interacting with the filesystem, and adds support for interacting with UNC or SMB paths

License: MIT License

C# 99.73% Dockerfile 0.27%

smbabstraction's Introduction

SmbAbstraction

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.

The project is curretly a work in progress and is not guaranteed to work for your specific application.

Usage

Examples

Example projects are available to view in SmbAbstraction.Examples

Dependency Injection

Registering Services

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureLogging(logging =>
        {
            logging.AddConsole();
            logging.AddDebug();
        })
        .ConfigureServices((hostContext, services) =>
        {
            services.AddSingleton<ISMBClientFactory>(new SMB2ClientFactory());
            services.AddSingleton<ISMBCredentialProvider>(new SMBCredentialProvider());

                    
            var serviceCollection = services.BuildServiceProvider();

            var clientFactory = serviceCollection.GetRequiredService<ISMBClientFactory>();
            var credentialProvider = serviceCollection.GetRequiredService<ISMBCredentialProvider>();
            var loggerFactory = serviceCollection.GetRequiredService<ILoggerFactory>(); //optional

            services.AddSingleton<IFileSystem>(new SMBFileSystem(clientFactory, credentialProvider, 65536u, loggerFactory));
        });

Making calls

With IDisposable

public void FileOpsInContext()
{
    var path = "valid_unc/smb_path";
    using (var credential = new SMBCredential("domain", "username", "password", path, _credentialProvider))
    {
        //FileInfo
        //_fileSystem.FileInfo.FromFileName(path)

        //DirectoryInfo
        //_fileSystem.DirectoryInfo.FromDirectoryName(path)

        //Stream
        //using (var stream = _fileSystem.File.Open(path, System.IO.FileMode.Open))
        //{
                    
        //}
    }
}

With Stored Credentials

You can add/cache credentials for a share so that you can operate them from raw IFileSystem calls

public void StoreCredentialsForShare(NetworkCredential credential)
{
    var domain = credential.Domain;
    var username = credential.UserName;
    var password = credential.Password;

    var sharePath = "valid_unc/smb_sharepath"; //ie. \\host\sharename or smb://host/sharename

    _credentialProvider.AddSMBCredential(new SMBCredential(domain, username, password, sharePath, _credentialProvider));
}

public void UseStoredCredentialsForFileOp()
{
    //FileInfo
    //_fileSystem.FileInfo.FromFileName(path)

    //DirectoryInfo
    //_fileSystem.DirectoryInfo.FromDirectoryName(path)

    //Stream
    //using (var stream = _fileSystem.File.Open(path, System.IO.FileMode.Open))
    //{

    //}
}

Raw

 static void Main(string[] args)
{
    var domain = "domain";
    var username = "username";
    var password = "password";

    var sharePath = "valid_unc/smb_share_path"; //ie. \\host\sharename or smb://host/sharename

    ISMBCredentialProvider credentialProvider = new SMBCredentialProvider();
    ISMBClientFactory clientFactory = new SMB2ClientFactory();
    IFileSystem fileSystem = new SMBFileSystem(clientFactory, credentialProvider, 65536u);

    //var path = _fileSystem.Path.Combine(sharePath, "test.txt");

    using (var credential = new SMBCredential(domain, username, password, sharePath, credentialProvider)) // NOTE: You can interchange path with sharePath here. 
    {                                                                                                     // SMBCredential will parse the share path from path
        //FileInfo
        //_fileSystem.FileInfo.FromFileName(path)

        //DirectoryInfo
        //_fileSystem.DirectoryInfo.FromDirectoryName(path)

        //Stream
        //using (var stream = _fileSystem.File.Open(path, System.IO.FileMode.Open))
        //{

        //}
    }
}

smbabstraction's People

Contributors

jo0 avatar jordanlytle avatar vitskr avatar woodscolby avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.