使用vb.net指定在记事本中写入的行位置 [英] Specify line position where to write in notepad using vb.net

查看:108
本文介绍了使用vb.net指定在记事本中写入的行位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码可以在记事本中写文本,但是我要在特定的行位置书写文本.我已经确定了行,但是我的问题是如何确定确切的行位置写在哪里?非常感谢您的帮助.

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()

推荐答案

记事本?你是认真的吗?您想在应用程序中编写一些对您来说陌生的文本吗? 不要这样做.

真是个坏主意.创建桌面应用程序并在其中使用TextBox更加容易.在文本框的某个位置进行书写也是相当糟糕的主意. 不要这样做.文本本质上是顺序的,不要滥用.最好使用ListBox.好的,如果您仍然想弄乱设计并写入特定的行,则必须计算定义为字符串System.Environment.NewLine的行终止符.您需要添加新行或计算现有行,具体取决于控件中已写入的文本. 再次,不要这样做!

如果您仍想进一步弄糟设计并使用真正的记事本(顺便说一句,编辑器不好吗?谁需要它?),可以使用Windows API SendInput模拟键盘输入.请参阅 http://msdn.microsoft.com/en-us/library/ms646310(v = vs.85).aspx [
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


总之,您不能.不是这样的.您这样做的方式使之成为不可能

在MSDN上查看
StreamWriter [如果文件存在且append为true,则将数据附加到文件.",这意味着所有数据都将写入文件中的现有数据之后.

尽管您可以访问StreamWriter.BaseStream,并且如果将报告CanSeek为true,则任何在文件原始末尾之前进行查找的尝试均将不起作用-它将引发异常.

唯一的方法是使用完整的FileStream,然后自行进行所有定位.
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.


这篇关于使用vb.net指定在记事本中写入的行位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆