ca2202

C# CA2000:Dispose objects before losing scope using FileStream/XmlTextReader

一笑奈何 提交于 2019-12-06 23:49:15
问题 I have lots of code like this: FileStream fs = File.Open(@"C:\Temp\SNB-RSS.xml", FileMode.Open); using (XmlTextReader reader = new XmlTextReader(fs)) { /* Some other code */ } This gives me the following Code Analysis warning: CA2000 : Microsoft.Reliability : In method 'SF_Tester.Run()', 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. If I follow the suggestion and I put the File.Open in a

C# CA2000:Dispose objects before losing scope using FileStream/XmlTextReader

北慕城南 提交于 2019-12-05 03:58:20
I have lots of code like this: FileStream fs = File.Open(@"C:\Temp\SNB-RSS.xml", FileMode.Open); using (XmlTextReader reader = new XmlTextReader(fs)) { /* Some other code */ } This gives me the following Code Analysis warning: CA2000 : Microsoft.Reliability : In method 'SF_Tester.Run()', 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. If I follow the suggestion and I put the File.Open in a using statement, I get this: CA2202 : Microsoft.Usage : Object 'fs' can be disposed more than once in

Disposing of object multiple times

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-01 06:21:31
I have the following code, which uses a stream to open and modify an Open XML document, and then save the new binary representation of that stream: MemoryStream stream = null; try { stream = new MemoryStream(); stream.Write(this.GetBinaryRepresentation(), 0, this.GetBinaryRepresentation().Length); using (WordprocessingDocument document = WordprocessingDocument.Open(stream, true)) { OfficeDocument.ModifyDocument(document); this.SetBinaryRepresentation(stream.ToArray()); stream = null; } } finally { if (stream != null) { stream.Dispose(); } } I had originally used two using blocks (one for the

Disposing of object multiple times

帅比萌擦擦* 提交于 2019-12-01 04:56:42
问题 I have the following code, which uses a stream to open and modify an Open XML document, and then save the new binary representation of that stream: MemoryStream stream = null; try { stream = new MemoryStream(); stream.Write(this.GetBinaryRepresentation(), 0, this.GetBinaryRepresentation().Length); using (WordprocessingDocument document = WordprocessingDocument.Open(stream, true)) { OfficeDocument.ModifyDocument(document); this.SetBinaryRepresentation(stream.ToArray()); stream = null; } }