+ Reply to Thread
Results 1 to 5 of 5

Thread: Reading and writing files using FileStream class

  1. #1
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    Reading and writing files using FileStream class

    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.

    Code:
    FileStream readingFile = new FileStream("first half of file.jpg", FileMode.Open);
    byte[] firstHalf = new byte[readingFile.Length];
    readingFile.Read(firstHalf, 0, firstHalf.Length);
    readingFile.Close();
    
    readingFile = new FileStream("second half of file.jpg", FileMode.Open);
    byte[] secondHalf = new byte[readingFile.Length];
    readingFile.Read(secondHalf, 0, secondHalf.Length);
    readingFile.Close();
    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.

    Code:
    FileStream writingFile = new FileStream("merged file.jpg", FileMode.Create);
    writingFile.Write(firstHalf, 0, firstHalf.Length);
    writingFile.Write(secondHalf, 0, secondHalf.Length);
    writingFile.Close();
    Afterwards, program will notify the user (using console) and open the folder where the source and merged files are. You can see for yourself.

    Code:
    Console.WriteLine("Will open the folder containing now.");
    Console.WriteLine("Press ENTER to continue...");
    Console.ReadLine();
    Process.Start(Environment.CurrentDirectory);


    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 Attached Thumbnails Reading and writing files using FileStream class-sshot-6.jpg   Reading and writing files using FileStream class-sshot-8.jpg  
    Attached Files Attached Files

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Reading and writing files using FileStream class

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

  4. #3
    Join Date
    Jul 2006
    Posts
    16,494
    Blog Entries
    75
    Rep Power
    143

    Re: Reading and writing files using FileStream class

    Very handy when you need to store a binary file in a database. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    sp3tsnaz's Avatar
    sp3tsnaz is offline Learning Programmer
    Join Date
    Sep 2009
    Posts
    41
    Rep Power
    0

    Re: Reading and writing files using FileStream class

    gg very nice tuto.. thankyou sire.. waiting for more ...

  6. #5
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    Re: Reading and writing files using FileStream class

    Thanks guys. And your welcome Sp3tsnaz.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. C# Tutorial: Reading and Writing XML Files
    By rueleonheart in forum CSharp Tutorials
    Replies: 3
    Last Post: 07-19-2011, 12:02 PM
  2. First writing and then reading, reopen ?
    By denarced in forum C and C++
    Replies: 2
    Last Post: 10-26-2010, 08:46 PM
  3. Reading and Writing Files in C
    By Guest in forum C Tutorials
    Replies: 39
    Last Post: 06-20-2010, 03:08 PM
  4. Reading and writing files using File methods
    By ArekBulski in forum CSharp Tutorials
    Replies: 15
    Last Post: 10-10-2009, 10:02 AM
  5. Reading / Writing Objects
    By ahmed in forum C# Programming
    Replies: 1
    Last Post: 10-08-2009, 06:01 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts