简体   繁体   English

C#MemoryStream到文本文件未完成

[英]C# MemoryStream to text file not Completing

I'm having an issue with using a memory stream on a large text file function in C#. 我在C#中的大型文本文件函数上使用内存流时遇到问题。 The output text file doesn't contain all the information, it's missing the last few lines of text that I wrote to it in streamwriter. 输出文本文件不包含所有信息,它缺少我在streamwriter中写入的最后几行文本。

Many of the posts say to use stream.flush() and reset the stream to the beginning of the stream using stream.seek(0, seekOrigin.End), but neither seem to make a difference. 许多帖子都说使用stream.flush()并使用stream.seek(0,seekOrigin.End)将流重置为流的开头,但似乎没有任何区别。

Here is the code: 这是代码:

using (var stream = new MemoryStream())
        {
            using (var sb = new StreamWriter(stream))
            {
                Decimal[,] oparray = new decimal[477, 521];

                List<Rech_RCH_Result> result1;

                //RCH File Header
                sb.WriteLine("#MODFLOW2000 Recharge Package");
                sb.WriteLine("PARAMETER 0");
                sb.WriteLine("         1        50");

                for (int sp = 1; sp < 3; sp++)  //should be 209
                {
                    ObjectResult<Rech_RCH_Result> qryRCH = db.Rech_RCH(NRD, sp);

                    result1 = qryRCH.Select(p => new Rech_RCH_Result { M_Col = p.M_Col, M_row = p.M_row, Recharge = p.Recharge, StressPeriod = p.StressPeriod }).ToList();

                    //Stress Period Header for SP
                    sb.WriteLine("         1         1");
                    sb.WriteLine("        18     1.000(10e15.6)                   -1     RECHARGE");

                    //model row = 1 to 476, col = 1 to 520
                    foreach (Rech_RCH_Result s in result1)
                    {
                        oparray[s.M_row.Value, s.M_Col.Value] = s.Recharge;
                    }

                    for (int x = 1; x < 477; x++)
                    {
                        for (int y = 1; y <= 520; y += 10)
                        {
                            //write out values in chunks of 10
                            sb.WriteLine(string.Format("   {0:0.000000E+00}   {1:0.000000E+00}   {2:0.000000E+00}   {3:0.000000E+00}   {4:0.000000E+00}   {5:0.000000E+00}   {6:0.000000E+00}   {7:0.000000E+00}   {8:0.000000E+00}   {9:0.000000E+00}", oparray[x, y], oparray[x, y + 1], oparray[x, y + 2], oparray[x, y + 3], oparray[x, y + 4], oparray[x, y + 5], oparray[x, y + 6], oparray[x, y + 7], oparray[x, y + 8], oparray[x, y + 9]));
                        }
                    }

                    Array.Clear(oparray, 0, oparray.Length);
                }

                stream.Flush();
                //Looks like the stream is not completing and missing a few lines.
                //stream.Seek(0, SeekOrigin.Begin);
                stream.Seek(0, SeekOrigin.End);


                using (FileStream file = System.IO.File.Create(Server.MapPath(string.Format("~/RCHFiles/{0}.txt", filename))))
                {
                    stream.WriteTo(file);
                }

                //using (ZipFile zip = new ZipFile())
                //{
                //    zip.AddEntry(filename + ".RCH", stream);
                //    zip.Save(Server.MapPath(string.Format("~/RCHFiles/{0}.zip", filename)));
                //}
            }
        }

You're flushing the MemoryStream , but not the StreamWriter . 您正在刷新MemoryStream ,而不是StreamWriter It's possible that that's buffering. 这可能正在缓冲。

Call sb.Flush() instead of stream.Flush() . 调用sb.Flush()而不是stream.Flush() (Flushing a MemoryStream doesn't do anything anyway.) (刷新MemoryStream不会做任何事情。)

Also note that you can use MemoryStream.CopyTo without rewinding, instead of Stream.WriteTo . 还要注意,您可以使用MemoryStream.CopyTo而不倒带,而不是Stream.WriteTo

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

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