jordanlytle / SmbAbstraction

Implements the System.IO.Abstractions interfaces for interacting with the filesystem, and adds support for interacting with UNC or SMB paths
MIT License
6 stars 8 forks source link

Examples aren't clear #34

Closed fayilt closed 4 years ago

fayilt commented 4 years ago

First of all, thank you for your efforts.

It would be nice to have more complete examples. I couldn't quite understand how credentials are used. The credentials object is created, but never used. Also, when calling FromFileName, do we specify the full path or just the file name? I tried with just the file name and it's trying to open a file in the current folder of the running application (e.g. c:\Sources\blah..)

using (var credential = new SMBCredential(domain, username, password, sharePath, credentialProvider)) // NOTE: You can interchange path with sharePath here
{
      var file = fileSystem.FileInfo.FromFileName(@"do we specify the full path here or just the file name?");
}
Jo0 commented 4 years ago

Creating and using the SMBCredential object adds it to SMBCredentialProvider, and upon disposal of SMBCredential it is then removed from SMBCredentialProvider. You also allowed to just add credentials for shares to SMBCredentialProvider using SMBCredentialProvider.AddSMBCredential(ISMBCredential) and removing credentials using SMBCredentialProvider.RemoveSMBCredential(ISMBCredential) at will.

If you are operating on an SMB/CIFS share, you will need to specify the full path.

UNC path - \\<hostname or ip>\<shareName>\<rest of path to directory or file>
SMB uri - smb://<hostname or ip>/<shareName>/<rest of path to directory or file>
using (var credential = new SMBCredential(domain, username, password, sharePath, credentialProvider))
{
      var fileInfoUNC = fileSystem.FileInfo.FromFileName(@"\\192.168.1.101\TestShare\TestFile.txt");
      var fileInfoSMB = fileSystem.FileInfo.FromFileName(@"smb://192.168.1.101/TestShare/TestFile.txt");

      var dirInfoUNC = fileSystem.DirectoryInfo.FromDirectoryName(@"\\192.168.1.101\TestShare\Test Dir");
      var dirInfoSMB = fileSystem.DirectoryInfo.FromDirectoryName(@"smb://192.168.1.101/TestShare/Test Dir");
}

If you are operating on the host file system, you feed paths how you normally would for typical System.IO calls

//Windows
using (var credential = new SMBCredential(domain, username, password, sharePath, credentialProvider))
{
      var fileInfo_CTemp = fileSystem.FileInfo.FromFileName(@C:\temp\TestFile.txt");
      var fileInfo_RelativeFile = fileSystem.FileInfo.FromFileName(@"..\..\somefile.txt");

      var dirInfo_CTemp = fileSystem.DirectoryInfo.FromDirectoryName(@"C:\temp");
      var dirInfo_relativeDir = fileSystem.DirectoryInfo.FromDirectoryName(@"..\..\..\some dir");
}

//Linux
using (var credential = new SMBCredential(domain, username, password, sharePath, credentialProvider))
{
      var fileInfo_CTemp = fileSystem.FileInfo.FromFileName(@"/home/someuser/test.txt");
      var fileInfo_RelativeFile = fileSystem.FileInfo.FromFileName(@"../../somefile.txt");

      var dirInfo_CTemp = fileSystem.DirectoryInfo.FromDirectoryName(@"/home/someuser/test_dir");
      var dirInfo_relativeDir = fileSystem.DirectoryInfo.FromDirectoryName(@"../../../some dir");
}
fayilt commented 4 years ago

thanks @Jo0 , that's very useful.