vb.net中,如何删除指定文本文档中的指定行的内容 50
展开全部
Dim newfile As New List(Of String)
For Each line As String In System.IO.File.ReadAllLines("TextFile1.txt")
If Not line.StartsWith("3") Then newfile.Add(line)
Next
System.IO.File.WriteAllLines("TextFile1.txt", newfile)
建个集合,用System.IO.File的ReadAllLines读出所有内容,逐个判断,如果是需要的加入集合,如果是要删除的什么都不做,最后用WriteAllLines写入即可。
这里说明一下,上面那个代码是用来删除所有以3开头的文本行。
2014-01-10
展开全部
把文件读到内存中,按换行符生成string类型的数组,然后执行删除操作,最后写回到文件。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
vb.net中删除指定文本文档中的指定行内容代码:
yVal hwnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As String _
) As Int32
Private Overloads Declare Auto Function SendMessage Lib "user32" ( _
ByVal hwnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As Int32 _
) As Int32
Private Const EM_GETLINE As Int32 = &HC4
Private Const EM_GETLINECOUNT As Int32 = &HBA
Private Const MAX_CHAR_PER_LINE As Int32 = &H100
Private Const EM_LINELENGTH As Int32 = &HC1
Private Const EM_LINEINDEX As Int32 = &HBB
Private Function GetLine(ByVal Text As TextBoxBase, ByVal Line As Integer) As String
Dim dwLineStart As Int32 = SendMessage(Text.Handle, EM_LINEINDEX, Line, 0)
Dim dwLineLen As Integer = SendMessage(Text.Handle, EM_LINELENGTH, dwLineStart, 0)
Dim Buff As String = Chr(dwLineLen And &HFF) & Chr(dwLineLen / &H100) & Space(dwLineLen)
Dim dwLen As Int32 = SendMessage(Text.Handle, EM_GETLINE, Line, Buff)
GetLine = Strings.Left(Buff, dwLen)
End Function
Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim g As Graphics = TextBox1.CreateGraphics()
Dim w As Integer = CType(g.MeasureString(TextBox1.Text, TextBox1.Font).Width, Integer)
Dim s As String = GetLine(TextBox1, 0)
MsgBox(s)
End Sub
yVal hwnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As String _
) As Int32
Private Overloads Declare Auto Function SendMessage Lib "user32" ( _
ByVal hwnd As IntPtr, _
ByVal wMsg As Int32, _
ByVal wParam As Int32, _
ByVal lParam As Int32 _
) As Int32
Private Const EM_GETLINE As Int32 = &HC4
Private Const EM_GETLINECOUNT As Int32 = &HBA
Private Const MAX_CHAR_PER_LINE As Int32 = &H100
Private Const EM_LINELENGTH As Int32 = &HC1
Private Const EM_LINEINDEX As Int32 = &HBB
Private Function GetLine(ByVal Text As TextBoxBase, ByVal Line As Integer) As String
Dim dwLineStart As Int32 = SendMessage(Text.Handle, EM_LINEINDEX, Line, 0)
Dim dwLineLen As Integer = SendMessage(Text.Handle, EM_LINELENGTH, dwLineStart, 0)
Dim Buff As String = Chr(dwLineLen And &HFF) & Chr(dwLineLen / &H100) & Space(dwLineLen)
Dim dwLen As Int32 = SendMessage(Text.Handle, EM_GETLINE, Line, Buff)
GetLine = Strings.Left(Buff, dwLen)
End Function
Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim g As Graphics = TextBox1.CreateGraphics()
Dim w As Integer = CType(g.MeasureString(TextBox1.Text, TextBox1.Font).Width, Integer)
Dim s As String = GetLine(TextBox1, 0)
MsgBox(s)
End Sub
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
思路:逐行读出文本文档内容写入内存,当读到指定行跳过。再从内存中写入硬盘存储。
追问
怎么跳过呢,我需要通过那一行内容判断是否删除的
追答
判断用if-then continue语句就可以了,你好像不怎么编程序啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
就是 操作 TXT 文本 嘛 ,搜索一下就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询