Code analysis. Do not delete the item several times.

I tried to follow the rules of code analysis using this method:

public static string Encrypt(string password) { string myPassword = string.Empty; if (!string.IsNullOrEmpty(password)) { myPassword = password; byte[] Value = System.Text.Encoding.UTF8.GetBytes(myPassword); SymmetricAlgorithm mCSP = new RijndaelManaged(); mCSP.Key = _key; mCSP.IV = _initVector; using (ICryptoTransform ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV)) { using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { using (CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write)) { cs.Write(Value, 0, Value.Length); cs.FlushFinalBlock(); cs.Close(); myPassword = Convert.ToBase64String(ms.ToArray()); } } } } return myPassword; } 

added all the blocks of Try {} Finaly{} , but he still yelled at me that I did not respect rule 2202. can anyone give me a hand with this?

yes, I read other posts on this subject and tried to apply it, but in the end I still get the same message.

+8
c # code-analysis
source share
4 answers

To get rid of CA2202 warning for cs , simply remove the call to its Close method.

Problem CA2202 for ms is more complex. The warning breaks because CryptoStream has the audacity to control the stream that it received through the constructor, which means that there is one inappropriate ms.Close() call that you cannot escape. The good news is that this untimely layout has no side effects in your case, and the same goes for the dual layout, so you can safely push the SuppressMessageAttribute and ignore the problem. (In cases where you really need to pass a stream in order to survive in its unpredictable location using something like CryptoStream , the usual method is to use a subclass of the stream, the purpose of which can be prevented by its instance code.)

+13
source share

Get rid of these two lines, they are not needed:

 cs.FlushFinalBlock(); cs.Close(); 
+2
source share

After documentation on this issue should lead to this code:

 public static string Encrypt(string password) { string myPassword = string.Empty; if (!string.IsNullOrEmpty(password)) { myPassword = password; byte[] Value = System.Text.Encoding.UTF8.GetBytes(myPassword); SymmetricAlgorithm mCSP = new RijndaelManaged(); mCSP.Key = _key; mCSP.IV = _initVector; using (ICryptoTransform ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV)) { System.IO.MemoryStream ms = null; try { ms = new System.IO.MemoryStream() var tmp = ms; using (CryptoStream cs = new CryptoStream(ms, ct, CryptoStreamMode.Write)) { ms = null; cs.Write(Value, 0, Value.Length); cs.FlushFinalBlock(); cs.Close(); myPassword = Convert.ToBase64String(tmp.ToArray()); } } finally { if(ms != null) ms.Dispose(); } } } return myPassword; } 
+2
source share

The documentation for this analysis warning ( http://msdn.microsoft.com/en-us/library/ms182334.aspx ) gives this example, similar to yours, that it controls threads:

 Stream stream = null; try { stream = new FileStream("file.txt", FileMode.OpenOrCreate); using (StreamWriter writer = new StreamWriter(stream)) { stream = null; // Use the writer object... } } finally { if(stream != null) stream.Dispose(); } 

but it still gives an error. The following is the error:

 Stream stream = null; StreamWriter writer = null; try { stream = new FileStream("file.txt", FileMode.OpenOrCreate); writer = new StreamWriter(stream)) // Do some stuff on the stream writer.. } finally { if(writer != null) writer.Dispose(); else if(stream != null) stream.Dispose(); } 

The reason is simple; if the author will always manage the flow for you. Only in the script is the script not created successfully if you manage the stream yourself. But I have to admit that I prefer the following syntax, and if you create a MemoryStream instead of a FileStream, the probability of an exception will be small, and I would prefer to suppress CA. Note that you can add using operators, so an extra level of nesting is often required.

 using (Stream stream = new FileStream("file.txt", FileMode.OpenOrCreate)) using (StreamWriter writer = new StreamWriter(stream)) { // Use the writer object... } 
+2
source share

All Articles