简体   繁体   English

使用VB.net动态编辑CSV文件的内容

[英]Dynamically edit Contents of a CSV file using VB.net

I have this code 我有这个代码

  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

To explain, the above code would check EACH line of a CSV file to check weather the contents has 14 commas('). 为了说明,上面的代码将检查CSV文件的每行以检查天气,其中内容有14个逗号(')。 Then if that line has more commas, the code will reduce it to 14, and if not, it would write commas so that it would be equal to 14. The above conditions are not created yet, so the code is just for testing. 然后,如果该行包含更多逗号,则代码会将其减少为14,否则,它将编写逗号以使其等于14。上述条件尚未创建,因此该代码仅用于测试。 I read something about WriteAllText and this code gives me the error : 我读了一些关于WriteAllText的内容,这段代码给了我错误:

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

Which, I think, means that I cant edit the CSV file because I'm currently using its data. 我认为这意味着我无法编辑CSV文件,因为我当前正在使用它的数据。

My question is, how could I edit the contents of the CSV file even when I am checking its contents? 我的问题是,即使我正在检查CSV文件的内容,如何编辑它的内容?

Please do disregard this code 请不要理会此代码

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

as I use this code just for testing, if ever I could manage to write it to the CSV file. 因为我仅将此代码用于测试,所以我可以设法将其写入CSV文件。

I Thank you for all your help. 感谢您的帮助。

If your CSV is not too big, you can read it in memory and work with it. 如果CSV不太大,则可以在内存中读取并使用它。 When you have finish you can write it again on disk. 完成后,可以再次将其写入磁盘。

    '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

The "Using" auto close the filestream or a streamreader/write or what you use. “使用”会自动关闭文件流或streamreader / write或您使用的文件。 Then you will not have problem like "file already in use". 这样,您将不会遇到“文件已在使用中”之类的问题。

Hope this help. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM