When running Code Analysis on my project I get the following warning:
CA2202 : Microsoft.Usage : Object ‘fs’ can be disposed more than once in method
I don’t see the problem, and the code compiles fine and produces the expected results in all test cases.
If I remove the
fs.Dispose(); statement then Code Analysis instead produces this warning:
object ‘fs’ is not disposed along all exception paths. Call System.IDisposable.Dispose on object ‘fs’ before all references to it are out of scope.
I am much grateful if you can review my code so that I can learn the proper way of disposing resources.
private string ReadFileContent(string filename, Encoding encoding, string category, FileArchive archiveConfig)
FileStream fs = null;
StreamReader sr = null;
string fileContent = string.Empty;
_logger.Debug("Reading file content of "" + category + """" file """" + filename + """""")