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.
Writing to FileStream from byte arraysCode: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();
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.
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:FileStream writingFile = new FileStream("merged file.jpg", FileMode.Create); writingFile.Write(firstHalf, 0, firstHalf.Length); writingFile.Write(secondHalf, 0, secondHalf.Length); writingFile.Close();
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.![]()
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
Very nice! I never can remember the namespaces either. +rep
Very handy when you need to store a binary file in a database. +rep
ggvery nice tuto.. thankyou sire.. waiting for more ...
Thanks guys. And your welcome Sp3tsnaz.![]()
proudly presenting my personal website and game website: F1Simulation. a thrilling Managed DirectX racing game... also my Ask Me
look at my tutorials about cropping images and Mono: bundling Mono with programs and lambda expressions
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks