Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Reading and writing files using FileStream class


  • Please log in to reply
4 replies to this topic

#1 ArekBulski


    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 03 September 2009 - 03:00 AM

Welcome members! This tutorial will show you how to read files into memory, and afterwards write them back to disk. I dedicate this tutorial to sp3tsnaz since he asked for explanation. Enjoy reading. :)

I am using Visual Studio 2008 Express Edition. The solution (by default) requires .NET Framework 3.5 to run.

Reading from FileStream to byte array

Before using FileStream class, I need to declare it's namespace. Since I never remember the namespaces I will write down "FileStream" and then a highlight will popup. Now, press Ctrl+. to open a menu, where you can add the namespace easily.


Now to the meritum, sp3tsnaz thread is about merging two files after they are downloaded. What is first needed is to read them both. I create a new FileStream (for every file) and specify name and mode. There are several modes, for example: FileMode.Open will open a file or throw an exception otherwise, FileMode.Create will create a new file or override existing one. Here is code that reads two files into memory, into byte arrays.

FileStream readingFile = new FileStream("first half of file.jpg", FileMode.Open);
byte[] firstHalf = new byte[readingFile.Length];
readingFile.Read(firstHalf, 0, firstHalf.Length);

readingFile = new FileStream("second half of file.jpg", FileMode.Open);
byte[] secondHalf = new byte[readingFile.Length];
readingFile.Read(secondHalf, 0, secondHalf.Length);

Writing to FileStream from byte arrays

Now it's time to write some data back to disk. We could merge the byte arrays into one, but it will not be necessary to write them into a single file. Here is some code that merges them into a file.

FileStream writingFile = new FileStream("merged file.jpg", FileMode.Create);
writingFile.Write(firstHalf, 0, firstHalf.Length);
writingFile.Write(secondHalf, 0, secondHalf.Length);

Afterwards, program will notify the user (using console) and open the folder where the source and merged files are. You can see for yourself. :)

Console.WriteLine("Will open the folder containing now.");
Console.WriteLine("Press ENTER to continue...");


Post your comments and share some +rep

I thank you in advance for your comments. They are always very rewarding, you will see when you write your own tutorials. If you need some help, then go ahead and ask. We are a community after all. :)

Attached Thumbnails

  • sshot-8.jpg
  • sshot-6.jpg

Attached Files

  • 2

#2 Guest_Jordan_*

  • Guest

Posted 03 September 2009 - 04:15 AM

Very nice! I never can remember the namespaces either. +rep
  • 0

#3 WingedPanther73


    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 03 September 2009 - 04:56 AM

Very handy when you need to store a binary file in a database. +rep
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/

#4 sp3tsnaz


    CC Regular

  • Member
  • PipPipPip
  • 42 posts

Posted 03 September 2009 - 08:11 AM

gg :D very nice tuto.. thankyou sire.. waiting for more ...
  • 0

#5 ArekBulski


    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 480 posts

Posted 06 September 2009 - 02:53 AM

Thanks guys. And your welcome Sp3tsnaz. :)
  • 0

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download