2016-11-26 29 views
0

動態編輯CSV文件的內容我有這樣的代碼使用VB.net

Dim fileReader As System.IO.StreamReader 
     fileReader = 
     My.Computer.FileSystem.OpenTextFileReader("Filepath") 

     Dim stringReader As String 
     'read csv file from first to last line 
     While fileReader.ReadLine <> "" 
      'get data of line 
      stringReader = fileReader.ReadLine() 
      'check the number of commas in the line 
      Dim meh As String() = stringReader.Split(",") 
      If meh.Length > 14 Then 

       My.Computer.FileSystem.WriteAllText("Filepath", "asd", True) 

      ElseIf meh.Length < 14 Then 

       My.Computer.FileSystem.WriteAllText("Filepath", "asd", True) 

      ElseIf meh.Length = 14 Then 

       MsgBox("This line of the file has " & stringReader & meh.Length & "commas") 

      End If 

     End While 
     End 
    End Sub 

爲了解釋,上面的代碼會檢查CSV文件,檢查天氣內容的每一行都有14個逗號(') 。然後,如果該行有更多的逗號,則代碼會將其減少到14,如果不是,則它將寫入逗號以便它等於14.上述條件尚未創建,因此代碼僅用於測試。我讀了一些關於WriteAllText這個代碼給我的錯誤:

The process cannot access the file 'filepath' because it is being used by another process. 

其中,我認爲,這意味着,因爲我目前使用它的數據我不能編輯CSV文件。

我的問題是,即使在檢查其內容時,如何編輯CSV文件的內容?

請不要忽略此代碼

My.Computer.FileSystem.WriteAllText("Filepath", "asd", True) 

,因爲我使用此代碼只是爲了測試,如果有的話我可以設法將其寫入到CSV文件。

我感謝您的幫助。

+0

您無法寫入已打開的文件進行讀取。除非文件非常大,否則可以使用File.ReadAllLines()加載所有數據,以便關閉文件並寫入文件。它看起來就是那個applet所做的,但通常你會想要關閉和處理流 - 不這樣做可能會導致相同的錯誤;和'End'不是退出NET應用程序的好方法。 – Plutonix

+0

對不起,'結束'@Plutonix我應該把它處理掉。你可能會建議什麼? –

+1

如果它是一個Winforms應用程序,主窗體上的Me.Close會結束它 – Plutonix

回答

1

如果您的CSV不是太大,您可以在內存中讀取它並使用它。完成後,可以在磁盤上再次寫入。

'Declare 2 List (or you can work directly with one) 
    Dim ListLines As New List(Of String) 
    Dim ListLinesNEW As New List(Of String) 

    'Read the file 
    Using MyCSVread As New IO.FileStream("C:\MyCSV.csv", IO.FileMode.Open, IO.FileAccess.ReadWrite) 

     'Read all the lines and put it in a list of T (string) 
     Using sReader As IO.StreamReader = New IO.StreamReader(MyCSVread) 
      Do While sReader.Peek >= 0 
       ListLines.Add(sReader.ReadLine) 
      Loop 
     End Using 
    End Using 

    'Your code for work with the line. Here you can write the new lines in the NEW list of string or work directly in the first 
    For L As Integer = 0 To ListLines.Count - 1 
     Dim meh As String() = ListLines(L).Split(",") 
     If meh.Length > 14 Then 
      'your code ... 
     ElseIf meh.Length < 14 Then 
      'your code ... 
     ElseIf meh.Length = 14 Then 
      MessageBox.Show("The line " & (ListLines(L) + 1) & " of the file has " & meh.Length & "commas", "MyApp", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) 
     End If 
    Next 

    'Open again the file for write 
    Using MyCSVwrite As New IO.FileStream("C:\MyCSV.csv", IO.FileMode.Open, IO.FileAccess.ReadWrite) 

     'Write back the file with the new lines 
     Using sWriter As IO.StreamWriter = New IO.StreamWriter(MyCSVwrite) 
      For Each sLine In ListLinesNEW.ToArray 
       sWriter.WriteLine(sLine) 
      Next 
     End Using 
    End Using 

「使用」會自動關閉文件流或流式讀取器/寫入或您使用的內容。那麼你將不會有像「文件已被使用」的問題。

希望得到這個幫助。

+0

我從來沒有想過我可以做這樣的事情!謝謝! –

+0

不客氣! – Tyler