2015-09-25 207 views
0

我需要在第30個位置的現有文本文件中寫入數據。我正在使用尋找,但不工作。使用下面的代碼。使用開始位置爲30.將數據寫入文本文件中的特定位置

Public Sub WriteToFile(ByVal data As DataTable, ByVal FileName As String, ByVal FieldLength As String, ByVal StartPosition As String) 
     Dim sbColumnData As New StringBuilder 

     Using writer = New StreamWriter(Directory.GetCurrentDirectory() + "\test.txt") 
      For Each oRecord In data.Rows 
       sbColumnData.Clear() 
       If oRecord(0).ToString().Length < FieldLength Then 
        sbColumnData.Append(oRecord(0).ToString().PadRight(FieldLength)) 
       ElseIf oRecord(0).ToString().Length = FieldLength Then 
        sbColumnData.Append(oRecord.ToString()) 
       Else 
        sbColumnData.Append(oRecord.ToString().Substring(0, FieldLength)) 
       End If 
       writer.Seek(0, StartPosition) 

       writer.WriteLine(sbColumnData) 
       writer.Flush() 
      Next 

     End Using 

    End Sub 

回答

相關問題