LGX1972·大∵熊

微信号:lgx1972_9Y

博客生活 首页 新随笔 联系 聚合 管理
  96 Posts :: 1 Stories :: 619 Comments :: 1 Trackbacks
本程序来源于网络上的VB6源程序,以下是我修改的,VB.NET可以使用。
'导入命名空间 
Imports VB = Microsoft.VisualBasic

'调用WIN-API

 Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As IntegerByVal lpBuffer As StringAs Integer
 
Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As StringByVal lpPrefixString As StringByVal wUnique As IntegerByVal lpTempFileName As StringAs Integer 

'该函数执行删除行,并返回删除的行数

Private Function 删除指定字符行(ByVal 文件名 As StringByVal 字符串 As StringAs Integer

        
Const MAX_PATH As Short = 260
        
Const NAME_LEN As Integer = MAX_PATH + 80

        
Dim inname As String
        
Dim strlen As Short
        
Dim outpath As String
        
Dim outname As String
        
Dim infile As Short
        
Dim outfile As Short
        
Dim one_line As String
        
Dim target As String
        
Dim deleted As Short

        
On Error GoTo DeleteLineError

        
' 打开输入文件

        inname = 文件名
        infile 
= FreeFile
()
        
FileOpen
(infile, inname, OpenMode.Input)

        
' 打开输出文件

        outpath = Space(NAME_LEN)
        strlen 
=
 GetTempPath(NAME_LEN, outpath)
        
If strlen = 0 Then

            
'MsgBox("临时文件路径错误.")
            Return 0
            
Exit Function
        
Else
            outpath 
= VB.Left(outpath, strlen)
        
End If


        outname 
= Space(NAME_LEN)
        
If GetTempFileName(outpath, "tmp"0, outname) = 0 Then

            
'MsgBox("临时文件名错误.")
            Return 0
            
Exit Function
        
End If

        strlen 
= InStr(outname, vbNullChar) - 1
        
If strlen > 0 Then outname = VB.Left(outname, strlen)
        outfile 
= FreeFile
()
        
FileOpen
(outfile, outname, OpenMode.Output)

        System.Windows.Forms.Application.DoEvents()

        
' 跳过指定字符的行,并复制内容到新文件

        deleted = 0
        target 
= 字符串
        
Do While Not EOF
(infile)
            one_line 
= LineInput
(infile)
            
If InStr(one_line, target) = 0 Then

                
PrintLine(outfile, one_line)
            
Else

                deleted 
= deleted + 1
            
End If
        
Loop

        
' 关闭输入和输出文件.
        FileClose(infile)
        
FileClose
(outfile)

        
' 删除源文件

        Kill(inname)

        
' 为新文件命名为源文件名

        Rename(outname, inname)

DeleteLineError:

       
Return
 deleted 
       
Exit Function


    
End Function
posted on 2010-06-26 02:11 LGX1972·大∵熊 阅读(905) 评论(1)  编辑  收藏 所属分类: 编程相关

Feedback

# re: VB.NET 删除文本文件中含有指定字符串的行 2010-06-26 15:41 干粉砂浆设备
一个在无爱的婚姻中苦苦煎熬着的女人,美好的爱情谁不渴望呢?  回复  更多评论