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.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.
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.
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.