SharpCifs.Std

** DEPRECATED! **

This library has only SMB version 1 features, which are not supported by the current Windows.

See: TalAloni's SMBLibrary implements SMB Client and Server, or my Simple Client Wrapper EzSmb.


Summary

This is a port of JCIFS to C#, SMB/CIFS(Windows shared folder) Access Library.
It works on Xamarin, .Net Core, and .Net Framework.

Source:
https://github.com/ume05rw/SharpCifs.Std

NuGet Package:
https://www.nuget.org/packages/SharpCifs.Std/

Description

SharpCifs.Std is a port from SharpCifs to .NET Standard 1.3.
Original SharpCifs: https://github.com/zinkpad/SharpCifs
It's a rework of SharpCifs, and The origin is JCIFS.

You can Read / Write / Scan the files on your Windows Shared Folders,
and Scan Workgroups, SMB-Servers on your LAN.

This is Free Library.
and probably the world's only SMB free library that can run on iOS without editing library code.

Required versions:
  • Xamarin.iOS 1.0
  • Xamarin.Android 1.0
  • .Net Core 1.0
  • .Net Framework 4.6

Installation

Visual Studio

In Solution Explorer, right-click "Reference" and select "Manage NuGet Packages".


In the NuGet Package Manager window, select "Browse".


In the search text box enter "sharpcifs", display the "SparpCifs.Std" package.
Click on the download mark to start adding packages.


A change confirmation window may be displayed.


Unless already installed, you will see a license confirmation of the Microsoft official package on which SharpCifs.Std depends.


Success, if "SharpCifs.Std" is listed in reference on Solution Explorer.



Xamarin Studio

From the header menu "Project", select "Add NuGet Packages".


The NuGet package search window will be displayed.
In the search text box, enter "sharpcifs".


The "SharpCifs.Std" package is listed.
Check it and click "Add Package" button.


Unless already installed, you will see a license confirmation of the Microsoft official package on which SharpCifs.Std depends.


Success, if "SharpCifs.Std" is listed in reference on Solution Explorer.



.Net Core

Open the *.csproj file of your project.(For .Net Core 1.1 or higher)


For .Net Core 1.0 series, open "project.json".


Since you need to specify the version, check the latest version on nuget.org.


Add the name and version of the "SharpCifs.Std" package in XML format to your *.csproj file.
(For .Net Core 1.1 or higher)


For .Net Core 1.0 series, Add infomation to "project.json".


Execute the "dotnet restore" command on your project folder, at the terminal.


The result of the command is displayed.
If it says "Restore completed" it is successful.



How to use

Get items in shared folder:

//using System;
//using SharpCifs.Smb;

//Get SmbFile-Object of a folder.
var folder = new SmbFile("smb://UserName:Password@ServerIP/ShareName/FolderName/");

//UnixTime
var epocDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

//List items
foreach (SmbFile item in folder.ListFiles())
{
    var lastModDate = epocDate.AddMilliseconds(item.LastModified())
                                .ToLocalTime();
    var name = item.GetName();
    var type = item.IsDirectory() ? "dir" : "file";
    var date = lastModDate.ToString("yyyy-MM-dd HH:mm:ss");
    var msg = $"{name} ({type}) - LastMod: {date}";
    Console.WriteLine(msg);
}

Connect & Auth:

//using System;
//using SharpCifs.Smb;

//Set Local UDP-Broadcast Port.
//When using the host name when connecting,
//Change default local port(137) to a value larger than 1024.
//In many cases, use of the well-known port is restricted.
//
// ** If possible, using IP addresses instead of host names 
// ** to get better performance.
//
SharpCifs.Config.SetProperty("jcifs.smb.client.lport", "8137");

//string to Auth-Object.
var auth1 = new NtlmPasswordAuthentication("UserName:Password");
var smb1 = new SmbFile("smb://192.168.0.1/ShareName/FolderName/", auth1);
Console.WriteLine($"exists? {smb1.Exists()}");

//3 string to Auth-Object.
var auth2 = new NtlmPasswordAuthentication(null, "UserName", "Password");
var smb2 = new SmbFile("smb://HostName/ShareName/FolderName/", auth2);
Console.WriteLine($"exists? {smb2.Exists()}");

//Insert auth info into URL.
var smb3 = new SmbFile("smb://UserName:Password@HostName/ShareName/FolderName/");
Console.WriteLine($"exists? {smb3.Exists()}");

//You can store authentication information in SharpCifs.Std.
SharpCifs.Config.SetProperty("jcifs.smb.client.username", "UserName");
SharpCifs.Config.SetProperty("jcifs.smb.client.password", "Password");

var smb4 = new SmbFile("smb://HostName/ShareName/FolderName/");
Console.WriteLine($"exists? {smb4.Exists()}");

Read a File:

//using System;
//using System.IO;
//using System.Text;
//using SharpCifs.Smb;

//Get target's SmbFile.
var file = new SmbFile("smb://UserName:Password@ServerIP/ShareName/Folder/FileName.txt");

//Get readable stream.
var readStream = file.GetInputStream();

//Create reading buffer.
var memStream = new MemoryStream();

//Get bytes.
((Stream)readStream).CopyTo(memStream);

//Dispose readable stream.
readStream.Dispose();
            
Console.WriteLine(Encoding.UTF8.GetString(memStream.ToArray()));

Create a new File:

//using System.Text;
//using SharpCifs.Smb;

//Get the SmbFile specifying the file name to be created.
var file = new SmbFile("smb://UserName:Password@ServerIP/ShareName/Folder/NewFileName.txt");

//Create file.
file.CreateNewFile();

//Get writable stream.
var writeStream = file.GetOutputStream();

//Write bytes.
writeStream.Write(Encoding.UTF8.GetBytes("Hello!"));

//Dispose writable stream.
writeStream.Dispose();

Scan Servers & Shares on LAN:

//using System;
//using SharpCifs.Smb;

//Set Local UDP-Broadcast Port.
//When using the host name when connecting,
//Change default local port(137) to a value larger than 1024.
//In many cases, use of the well-known port is restricted.
//
// ** If possible, using IP addresses instead of host names 
// ** to get better performance.
//
SharpCifs.Config.SetProperty("jcifs.smb.client.lport", "8137");

//Get local workgroups.
var lan = new SmbFile("smb://", "");
var workgroups = lan.ListFiles();

foreach (var workgroup in workgroups)
{
    Console.WriteLine($"Workgroup Name = {workgroup.GetName()}");
    try
    {
        //Get servers in workgroup.
        var servers = workgroup.ListFiles();
        foreach (var server in servers)
        {
            Console.WriteLine($"{workgroup.GetName()} - Server Name = {server.GetName()}");
            try
            {
                //Get shared folders in server.
                var shares = server.ListFiles();
                foreach (var share in shares)
                {
                    Console.WriteLine($"{workgroup.GetName()}{server.GetName()} - Share Name = {share.GetName()}");
                }
            }
            catch (Exception)
            {
                Console.WriteLine($"{workgroup.GetName()}{server.GetName()} - Access Denied");
            }
        }
    }
    catch (Exception)
    {
        Console.WriteLine($"{workgroup.GetName()} - Access Denied");
    }
}

Name Resolution:

//using System;
//using System.Net;
//using SharpCifs.Netbios;

//Set Local UDP-Broadcast Port.
//When using the host name when connecting,
//Change default local port(137) to a value larger than 1024.
//In many cases, use of the well-known port is restricted.
//
// ** If possible, using IP addresses instead of host names 
// ** to get better performance.
//
SharpCifs.Config.SetProperty("jcifs.smb.client.lport", "8137");

var naddr = NbtAddress.GetByName("HostName");
IPAddress addr = naddr.GetInetAddress();
Console.WriteLine($"IP = {addr}");

Setting Paramaters:

//Set Local IP Address.
//If a connection error occurs(ex: Failed to connect: [NET BIOS NAME]),
//Try to set the Local IP Address.
//When the host name of the device is invalid as the DNS name,
//it may be impossible to determine the local address.
SharpCifs.Config.SetProperty("jcifs.smb.client.laddr", "192.168.0.2");

//Set Local UDP-Broadcast Port.
//When using the host name when connecting,
//Change default local port(137) to a value larger than 1024.
//In many cases, use of the well-known port is restricted.
//
// ** If possible, using IP addresses instead of host names 
// ** to get better performance.
//
SharpCifs.Config.SetProperty("jcifs.smb.client.lport", "8137");

//You can store authentication information in SharpCifs.Std.
SharpCifs.Config.SetProperty("jcifs.smb.client.username", "UserName");
SharpCifs.Config.SetProperty("jcifs.smb.client.password", "Password");

//If you are not using DFS(Distributed File System),
//Disabling DFS improves the speed of NetBios name resolution.
SharpCifs.Config.SetProperty("jcifs.smb.client.dfs.disabled", "true");

//and more configuration parameters are in the official JCIFS site.
//https://jcifs.samba.org/src/docs/api/overview-summary.html

Refresh Settings & Connection Caches:

//----------------------------------------
// Required: Version 0.2.9 or later.
//----------------------------------------
//
//Force apply the setting values.
//On the default operation, the setting values at the time of first SMB-processing is applied, and It CAN NOT BE CHANGED.
//This method forcibly applies the setting values changed after running SMB processing.
//
//And, This method disposes cached connections that are currently invalid.
//For example, connections that did not work within 30 minutes after connecting are disposed.
SmbFile.Initialize();

//Reset invalid connections.
//Use this method to disposed the invalid connections without manipulating the setting values.
SmbTransport.ClearCachedConnections();

//Reset all(includes active) connections.
//Use this method to dispose all connections, including currently connected.
//Be careful, It will dispose connections even during data transfer.
SmbTransport.ClearCachedConnections(true);


Showcase

ComicLAN

It's comics reader app for iOS, implemented by Xamarin.iOS.
https://itunes.apple.com/us/app/comiclan-necessary-enough-thats-comic-reader/id1252927463?l=ja&ls=1&mt=8


[Cite Image Source]
Title:Give My Regards to Black Jack
題名:ブラックジャックによろしく
Author:SHUHO SATO
著作者:佐藤秀峰


Licence


Links

This Project: GitHub - ume05rw/SharpCifs.Std

https://github.com/ume05rw/SharpCifs.Std

GitHub - zinkpad/SharpCifs: SharpCifs is a port of JCIFS to C#

https://github.com/zinkpad/SharpCifs

JCIFS - The Java CIFS Client Library

https://jcifs.samba.org/