Question streamwriter writes to a specified line#

dmrac2006

Member
Joined
Jul 25, 2006
Messages
5
Programming Experience
5-10
Group,
I am needing to write to a "rtf" file, and always insert my text on line 3 of either a new or an existing rtf document.
I'm familiar with vb, but have not done much with the system.io functions. I have been testing several examples from the internet using the streamreader and streamwriter but still having issues finding a way to "insert text" on a specified line number. My questions:

1)I will always need to write on line #3, do I need to read all lines as this example shows "lines.AddRange(System.IO.File.ReadAllLines("C:/test/myrtffile.rtf"))"

OR

2)Is there a way to simply specify a write and include the line# to write it on, something such as: "write.WriteLine(mytext + vbCrLf, 3)" - the "3" indicating line three. When the write happens on an "existing" rtf document, I want to insert at line 3 and push existing text down the page.

I'm not sure that I need to read text if I know the exact line number that I will always insert my text on.

Any suggestions would be greatly appreciated!!
 
jmcilhinney,
Many thanks for your reply!! I have done many examples this week trying to understand the system.io and you have helped me with the read/write concept.

My next question, positioning myself on line 3. I have tried an example with the "ReadAllLines", ->

lines.AddRange(System.IO.File.ReadAllLines("C:/mydata/test.rtf")) - with this example I was able to control the lines with the array and the example worked great ->

lines.Item(2) = lines.Item(2) + vbCrLf + "this is a test for line 3 print" + vbCrLf

EXCEPT...(and I do not understand this)...

(these are all rtf documents that I will be working with)
IF I create a "test.rtf" in word pad, my program works perfect.
IF I create a "test.rtf" in MS WORD, my program "corrupts" the document, the only way to open it is in notepad - and when I do......I get many, many, lines of this....(is it code page character that comes along with MS WORD??)

{\rtf1\adeflang1025\ansi\ansicpg1252\uc1\adeff0\deff0\stshfdbch0\stshfloch0\stshfhich0\stshfbi0\deflang1033\deflangfe1033{\fonttbl{\f0\froman\fcharset0\fprq2{\*\panose 02020603050405020304}Times New Roman;}{

So of all examples that I have tried, the only one that I have been able to "write" on line 3, is not working with the MS WORD rtf documents. The other examples that I have used, inserts at the top, at the bottom, or completely replaces the text - but the insert at position is where I am having my trouble with.

I'll take more examples if you have any to share.

Many Thanks again for your reply!!
 
If your file is RTF then you can't really just use ReadAllLines because that will just read it as plain text, including all the RTF markup. RTF markup works in a similar way to HTML markup, although the format is rather different.

As much as I recommend to steer clear of using controls for non-UI operations, this is one instance where that may be your best bet. Unless you want to actually learn RTF and parse it in code yourself, your best bet may be to load the file into a hidden RichTextBox, which will parse the RTF for you. You can then access the text itself, regardless of any formatting, using the Text and Lines properties.
 
rich text format

jmcilhinney:
Again - Thanks For Your Reply!!
and again - I have more questions:

Your comment:
1) "If your file is RTF then you can't really just use ReadAllLines because that will just read it as plain text, including all the RTF markup. RTF markup works in a similar way to HTML markup, although the format is rather different."

You have just explained to me what I have been searching for - I understand this, since ReadAllLines reads all as plain text, that is why I'm getting all that extra text with the MS Word documents, it's no longer formatting marks, it's text. THANK YOU!!


2) "As much as I recommend to steer clear of using controls for non-UI operations, this is one instance where that may be your best bet. Unless you want to actually learn RTF and parse it in code yourself,"
--I'm interested, but I need some resources, do you recommend a good book?? or links??

3) "your best bet may be to load the file into a hidden RichTextBox, which will parse the RTF for you. You can then access the text itself, regardless of any formatting, using the Text and Lines properties."
- Even doing this, I'm a little unclear, I'll keep searching sites for examples.

Many Thanks!
Diane
 
Back
Top