SharpCifs.Std icon indicating copy to clipboard operation
SharpCifs.Std copied to clipboard

Examples should use `using`

Open zspitz opened this issue 7 years ago • 2 comments

Wrapping code in a using block is the standard way in .NET to free up resources.

For example, instead of the current sample code for reading a file, how about this:

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.
using (var readStream = file.GetInputStream()) {

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

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

Console.WriteLine(Encoding.UTF8.GetString(memStream.ToArray()));

zspitz avatar Jan 30 '18 19:01 zspitz

hmm, Personally, the shorter the sample, the more pleasing I am. But perhaps, there are many copy and paste programmers in the world than we think. I think that you are right.

I'll update samples at a convenient time.

ume05rw avatar Feb 04 '18 09:02 ume05rw

Using idiomatic C# is shorter in this case (no explicit call to Dispose), than the same example in the readme.

zspitz avatar Feb 05 '18 03:02 zspitz