VB .NET-文本文件读写

读取指定分隔符格式文本文件:

Using MyReader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\TestFolder\test.txt")
   MyReader.TextFieldType = FileIO.FieldType.Delimited
   MyReader.SetDelimiters(",")
   Dim currentRow As String()

   While Not MyReader.EndOfData
      Try
         currentRow = MyReader.ReadFields()
         Dim currentField As String
         For Each currentField In currentRow
            MsgBox(currentField)
         Next
      Catch ex As Microsoft.VisualBasic.
                  FileIO.MalformedLineException
        MsgBox("Line " & ex.Message &
        "is not valid and will be skipped.")
      End Try
   End While

End Using

Using  根据需要获取该代码块控制的系统资源 

Using sw As StreamWriter = New StreamWriter(fileName, True, fileEncoding)
    If Not IsNothing(fileModel.AddInfo) Then                    
        For Each addInfo As String In fileModel.AddInfo                 
            sw.WriteLine(addInfo)                   
        Next                    
    End If                  


    If hasHeader Then                   
        sw.WriteLine(fileModel.Header)                  
    End If                  


    Me.CSVFileFromArray(fileModel.DataBody, sw, blnAddDoubleQuotes)                 


    If displayMsg Then                  
        Message.Show("I1001", "CSV")                    
    End If                  

    Return True                 
End Using

 

你可能感兴趣的:(Visual,Basic,.NET)