Click here to Skip to main content
14,695,620 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have this code to write text in the notepad but what I want to do is to it in specific line position. I have already identify the row but my problem how can i determine exact line position where to write? Your help is much appreciated.

Dim objStreamWriter As StreamWriter
   Dim x As Long

   'Open the file.
   objStreamWriter = New StreamWriter(Filename_Doc, True, Encoding.Unicode)

   'Write out the numbers 1 through 10 on the same line.
   For x = 1 To 10
     if x=5 then
       objStreamWriter.Write(x)
     end if
   Next x

   'Close the file.
   objStreamWriter.Close()
Posted
Comments
Sandeep Mewara 11-May-11 2:43am
   
Not clear.

Notepad? Are you serious? You want to write some text into the the application foreign to yours? Don't do it.

Really bad idea. It's much more easy to create a desktop application and use a TextBox in it. Writing in a certain position of a text box is pretty bad idea, too. Don't do it. The text is sequential thing in its nature, don't abuse it. Better use ListBox. OK, if you still want to screw up the design and write to a specific line, you have to count line terminators defined as the string System.Environment.NewLine. You need to either add new lines or count existing ones, depending on the text already written in the control. Again, don't do it!

If you still want to screw up the design even more and use real Notepad (what a bad editor, by the way? who needs it?), you can use Windows API SendInput to simulate key input. See http://msdn.microsoft.com/en-us/library/ms646310(v=vs.85).aspx[^]. Again, don't do it!

Is is really so hard to devise a nice, simple and correct design? What's is your ultimate purpose? I don't think you want what you think you want; more likely you just don't know better ways. If you share what you want to achieve, you can probably get a better advice.

—SA
   
Comments
cuteband 11-May-11 2:51am
   
yeah its really challenging work as i need to count each line but thats the requirement. I already did this using word doc and i can setup it very easily. But again thats the requirement of my project anyway i already solved it.
Sergey Alexandrovich Kryukov 11-May-11 15:15pm
   
There is almost nothing challenging here if you have more clear design.
I'm telling you, use something like a ListBox in your application and don't create a challenge on an empty place.
--SA
In short, you can't. Not like that. The way you are doing this makes it impossible

Looking at StreamWriter on MSDN[^] it specifically says "If the file exists and append is true, the data is appended to the file." which means that all data will be written after existing data in the file.

Although you can access the StreamWriter.BaseStream and if will report that CanSeek is true, any attempt to seek back before the original end of the file will not work - it will throw an exception.

The only way to do this it to use a full FileStream, and do all the positioning yourself.
   
Comments
cuteband 11-May-11 2:55am
   
Solution no. 2 already works for me. thanks

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