Click here to Skip to main content
15,903,012 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi to all,

I am checking a notepad file which contains almost 5000 lines of records.
I have to insert 10 rows of records after each 500 lines in the same file.
After doing the job I will have a file which will contain total 5100 lines of records.
Original File
00500 <-- New 10 rows after this record

I am using System.IO namespace for reading purpose.


Framework = 2.0
Language = VB.NET
Please Advice.
Thanks In Advance.
Updated 4-Jan-12 1:35am

There's no 'insertion' functionality for files: you have to create a new file with the appropriate content:
Read line by line from input file (you may use the StreamReader.ReadLine Method[^]) and output the read line to the ouput file, until line count reaches 500.
Then write the '10 rows' to the output file and, finally, resume reading line by line the input file (and write every line to the output file) until the end-of-(input)file.
Share this answer
Wendelius 4-Jan-12 17:51pm    
That'll be a good way. 5. I added a bit brute-force solution...
Harshad-HBK 6-Jan-12 0:26am    
Thanks CPallini..I got ma solution
CPallini 7-Jan-12 15:55pm    
You are welcome.
One quite brute-force way is to read all the lines to an array or a list, add necessary items and then write the contents to the file. For example something like:
List<string> lines = System.IO.File.ReadAllLines("...").ToList<string>();
lines.Insert(500, "some string");
lines.Insert(501, "other string");
Share this answer
CPallini 4-Jan-12 18:19pm    
My 5.
Wendelius 4-Jan-12 18:41pm    
Thanks :)
Harshad-HBK 6-Jan-12 0:27am    
Thanks Mika, job done successfully...
Wendelius 6-Jan-12 6:10am    
You're welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900