简体   繁体   English

StreamReader和Streamwriter

[英]StreamReader and streamwriter

what mean this exception please ? 请问这个例外是什么意思?

Unhandled Exception: System.ObjectDisposedException: The object was used after being disposed. 未处理的异常:System.ObjectDisposedException:在处置对象后使用了该对象。 at System.IO.StreamWriter.Write (System.String value) [0x00000] in :0 at System.IO.TextWriter.WriteLine (System.String value) [0x00000] in :0 at fichier.MainClass.Main (System.String[] args) [0x000bd] in /Users/mediatun1/Projects/fichier/fichier/Main.cs:122 在System.IO.StreamWriter.Write(System.String值)[0x00000]在System.IO.TextWriter.WriteLine(0.00000)在0处在fichier.MainClass.Main(System.String /Users/mediatun1/Projects/fichier/fichier/Main.cs中的[] args)[0x000bd]:122

  System.Security.Cryptography.MD5 alg = System.Security.Cryptography.MD5.Create();
    System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
    byte[] md5val = alg.ComputeHash(enc.GetBytes("TESTTESTTESTTESTTESTTEST"));
    string output = Convert.ToBase64String(md5val);
    string pathPlainTextFile="/Users/mediatun1/Desktop/IAAT.xml"; 
    string pathCypheredTextFile="/Users/mediatun1/Desktop/IAA.xml";
    StreamReader fsPlainTextFile = File.OpenText(pathPlainTextFile);

    FileInfo t = new FileInfo(pathCypheredTextFile);
    StreamWriter Tex =t.CreateText();
    string input = null;
    while ((input = fsPlainTextFile.ReadLine()) != null)
    {


                  byte[] plainText = Encoding.UTF8.GetBytes(input);



                RijndaelManaged rijndael = new RijndaelManaged();

                // Définit le mode utilisé
                rijndael.Mode = CipherMode.ECB;

                // Crée le chiffreur AES - Rijndael
                ICryptoTransform aesEncryptor = rijndael.CreateEncryptor(md5val,null);

                MemoryStream ms = new MemoryStream();

                // Ecris les données chiffrées dans le MemoryStream
                CryptoStream cs = new CryptoStream(ms, aesEncryptor, CryptoStreamMode.Write);
                cs.Write(plainText, 0, plainText.Length);
                cs.FlushFinalBlock();


                // Place les données chiffrées dans un tableau d'octet
                byte[] CipherBytes = ms.ToArray();


                ms.Close();
                cs.Close();

                // Place les données chiffrées dans une chaine encodée en Base64

    Tex.WriteLine (Convert.ToBase64String(CipherBytes));

                Console.WriteLine (Convert.ToBase64String(CipherBytes));
            Tex.Close();    


    }

You have Tex.Close(); 你有Tex.Close(); inside your loop. 在你的循环中。 So after iteration 1 the StreamWriter is closed. 因此,在迭代1之后,StreamWriter将关闭。

Generally Streams that are opened outside the loop should be closed outside the loop. 通常,在循环外部打开的Streams应在循环外部关闭。

Sounds like an issue with closing your streams to early.I cannot quite work it out on your code, perhaps if you change order of 听起来像是关闭流的问题。我无法完全在您的代码上解决它,也许如果您更改顺序

ms.Close();
cs.Close();

to... 至...

cs.Close();
ms.Close();

it may work 它可能有效

EDIT: As jaywayco has pointed out, the failing code is definitely going to be due to Tex being closed inside the loop. 编辑:正如jaywayco所指出的,失败的代码肯定是由于Tex在循环内被关闭。 I had completely missed this whilst trying to understand the code 我在尝试理解代码时完全错过了

However, you really need to tidy all this up. 但是,您确实需要整理所有这些内容。 You should use "using" statements for your streams as this will close them automatically when finished with. 您应该对流使用“ using”语句,因为这将在完成后自动关闭它们。 Something like.... 就像是....

using(MemoryStream ms = new MemoryStream(...))
{
   using(CryptoStream cs = new CryptoStream(...))
   {
      //code for cs stream in here
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM