Csharp write memorystream to file


















Post your question to a community of , developers. Sign in Join Now. New Post. Alexander Muylaert. Hi What would be the easiest way to save a memorystream to a file? Kind regards Alexander. Follow Post Reply. Tamir Khason. Convert it to binary filestream or just write byte ofter byte.

ToArray ; fileStream. Write data, 0, data. Length ; That's relatively inefficient though, as it involves copying the buffer. It's fine for small streams, but for huge amounts of data you should consider using: fileStream.

Write memoryStream. GetBuffer , 0, memoryStream. Position ; assuming the memory stream is already positioned at the end. John Wood. OpenWrite "blah. Write ms. ToArray ; fs.

CopyTo file ;. Jon Skeet Jon Skeet 1. No CopyTo method appears on stream, I am using. NET Framework 3. It is only available from. NET Framework 4. You could use MemoryStream. GetBuffer instead, for example. I agree, very sorry.

And a lot of thanks, you save my day! Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.

Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta. New post summary designs on greatest hits now, everywhere else eventually. Related Hot Network Questions. Question feed. Length ; fs. Write ms. ToArray , 0, ms. Length ; ms. Close ; fs. Flush ; fs. Coding Duchess Coding Duchess 5, 13 13 gold badges 86 86 silver badges bronze badges. Not saying that this will fix your problem, but consider cleaning up the code by using Stream.

CopyTo Method. Length ; Why are you reading the memory stream, overwriting whatever was put in there by dcjs.

That seems like your problem there, don't reset the stream position and write over itself. Why are you writing to the MemoryStream before the FileStream? But with. CopyTo Stream. I tried ms. CopyTo fs but gotten same result — Coding Duchess. ElenaDBA The problem isn't your file writing. The problem is in your Serializer. Add a comment. Active Oldest Votes. There is a very handy method, Stream. WriteLine "asdasdasasdfasdasd" ; writer. Seek 0, SeekOrigin. CopyTo fs ; fs. Nothing happened at all - the file was unchanged — Coding Duchess.

I updated the code with a modification, you need to use Seek 0, SeekOrigin. I'm using. Net Framework 3.



0コメント

  • 1000 / 1000