Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

Problems writing MemoryStream to disk

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tim Johnson - 02 Oct 2006 22:36 GMT
I offer 2 Write methods in my class, one that writes to and returns a
MemoryStream, and one that writes to a file.  The 2nd one just calls the 1st
one, then I want to write the returned ms to disk.  I tried 2 different
approaches, with the problems indicated:

(1)
        using (FileStream fs = File.OpenWrite(fileName))
        {
           fs.Write(ms.GetBuffer(), 0, (int)ms.Position);
        }

==> This approach wouldn't write out the final block of  text (about 500
bytes), even though I did a Flush on the ms.

(2)
        byte[] array = ms.ToArray();
        using (MemoryStream ms2 = new MemoryStream(array))
        {
           using (FileStream fs = File.OpenWrite(fileName))
           {
              fs.Write(array, 0, ms2.Length);
           }
        }
==> This approach would write out an extra 200K of garbage text!

So I had to resort to this uglier brute-force method which at least works:

        byte[] array = ms.ToArray();
        using (MemoryStream ms2 = new MemoryStream(array))
        {
           using (StreamReader sr = new StreamReader(ms2))
           {
              using (StreamWriter sw = new StreamWriter(fileName, false))
              {
                 string line;
                 while ( (line=sr.ReadLine()) != null)
                    sw.WriteLine(line);
              }
           }
        }

Any ideas why one of the first more compact approaches would not be reliable?
Vadym Stetsyak - 02 Oct 2006 22:54 GMT
Hello, Tim!
You wrote  on Mon, 2 Oct 2006 14:36:02 -0700:

[skipped]

TJ> Any ideas why one of the first more compact approaches would not be
TJ> reliable?

You can try this:

MemoryStream memStream; //memory stream which contents
//will be written to the file

using (FileStream fs = File.OpenWrite(fileName))
{
   memStream.WriteTo(fs);
   fs.Close();
}

With best regards, Vadym Stetsyak.
Blog: http://vadmyst.blogspot.com
Tim Johnson - 03 Oct 2006 00:10 GMT
Thanks, that seems to do the trick!

Tim

> Hello, Tim!
> You wrote  on Mon, 2 Oct 2006 14:36:02 -0700:
[quoted text clipped - 17 lines]
> With best regards, Vadym Stetsyak.
> Blog: http://vadmyst.blogspot.com 
Jon Skeet [C# MVP] - 02 Oct 2006 23:58 GMT
> I offer 2 Write methods in my class, one that writes to and returns a
> MemoryStream, and one that writes to a file.  The 2nd one just calls the 1st
> one, then I want to write the returned ms to disk.  I tried 2 different
> approaches, with the problems indicated:

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.