I'm going to create a new thread for this as previous accepted answers no longer work as of VS2012 and up. When using nested using statements, Visual Studio code analysis gives you the annoying CA2202 Do not dispose objects multiple times, for the following code:
using (MemoryStream msData = new MemoryStream(encodedData))
{
using (BinaryWriter wtr = new BinaryWriter(msData))
{
wtr.Write(IV, 0, IV.Length);
wtr.Write(encrypted, 0, encrypted.Length);
}
}
This is annoying, because it is even listed in MSDN samples. Microsoft even has a recommended fix for this warning, however it no longer fixes the warning. This may or may not work for you, depending on what Visual Studio version/compiler you are using.