Windows Develop Bookmark and Share   
 index > Windows Forms Sample Applications > Merging two files (only just started using win forms)
 

Merging two files (only just started using win forms)

Hi there,

I would say im quite compitent and confident with using c# in .net websites etc. and have recently adapted my skills to try programing some windows forms.

I was wondering if I could get some guidence.

What I would like to acheive is joining 2 files together in a particular way. This would involve "opening" file 1, and "opening" file 2, and then choosing a "save to" location.

eg.

Contents of FILE 1:
Lorem ipsum dolor sit amet, nec at dignissim diam ornare neque urna, quis aptent nec

Contents of FILE 2:
The quick brown fox jumps over the lazy dog

I would like to join the 2 files in the following way, so the outputted file contents ended up looking like this:
Hello everyone how are you The quick brown Lorem ipsum dolor sit amet, nec at dignissim fox jumps over the lazy dog diam ornare neque urna, quis aptent nec thank you

As you can see, i'd like to join parts of each file together, and also add in a little of my own too. The contents of File 1 and File 2 will be different, but i will always join at the same length/distance from the start (eg. the 27th character will always be the begining of the the contents from file 2 etc etc.)

Any help please? or some guidence would be great.

Thanks in advanced.

Neil-One  Friday, February 08, 2008 12:00 PM
You can accomplish this with the StreamReader and StreamWriter classes. Read the necessary amount of data from each file, then write to the output.

-Ryan / Kardax
Ryan Lamansky  Friday, February 08, 2008 10:08 PM

Thats for your guidence Ryan. All I needed was a point in a right direction and I got going.

I am able to read a text file to stream and output it into a textbox if i want etc. (using StreamReader and StreamWriter)

So far I have figured out how to 'read' a binary file and how to 'write' it too, (i have in effect made a file copier). (this used FileStream.OpenRead and FileStream.OpenWrite)

My example was written in a way so it could be easily understood, however the file that i would like to 'amend' is actually a binary file (non-text). So i tried using the File Stream as i mentioned above. What my aim actually is, is to append some information to the binary. Do i need to find out the hex memory addresses in the file or can i directly insert 'additional information' directly into the binary and save it using FileStream.OpenWrite?

Neil-One  Wednesday, February 13, 2008 2:03 PM
You can either use the stream's .Seek method or set the Position property to change the position of reading or writing in a file. It's recommended to do this before applying a StreamReader or StreamWriter, because both of those work through buffers that could become inaccurate if the file position changes underneath them.

There are no mechanisms to "insert" data into the middle of the stream (this would require, at a minimum, rewriting the rest of the file after the inserted data), but you can append simply by Seeking to the end of the stream before beginning your writes.

-Ryan / Kardax
Ryan Lamansky  Friday, February 15, 2008 8:30 PM

You can use google to search for other answers

Custom Search

More Threads

• Adding field to add task
• TaskVision as Document WorkFlow
• dataservice.asmx
• CImage Capture on Internet Explorer after
• Urgent -- Using ComboBox/TextBox, I want to convert into operator for Oracle Database [Plz Refer Detail]
• Invalid 'integrated security' value in the connection string
• Please try out this software
• Terrarium .NET 2.0
• Left pane problem -- newbie question
• Localization form