ume05rw / SharpCifs.Std

SharpCifs.Std is a port from SharpCifs to .NET Standard. Original SharpCifs: https://github.com/zinkpad/SharpCifs
GNU Lesser General Public License v2.1
86 stars 34 forks source link

Examples should use `using` #15

Open zspitz opened 6 years ago

zspitz commented 6 years ago

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()));
ume05rw commented 6 years ago

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.

zspitz commented 6 years ago

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