在VB.NET中更新CSV文件

时间:2017-02-27 22:28:47

标签: vb.net csv

我在CSV文件信息显示中使用以下代码给出数据网格:

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    Dim sr As New IO.StreamReader("C:\Data.csv")

    Dim dt As New DataTable

    Dim newline() As String = sr.ReadLine.Split(","c)

    dt.Columns.AddRange({New DataColumn(newline(0)),

                         New DataColumn(newline(1)),

                         New DataColumn(newline(2))})

    While (Not sr.EndOfStream)

        newline = sr.ReadLine.Split(","c)

        Dim newrow As DataRow = dt.NewRow

        newrow.ItemArray = {newline(0), newline(1), newline(2)}

        dt.Rows.Add(newrow)

    End While

    dgView.DataSource = dt

End Sub

我将使用下面的代码更改它,但显示的消息是:

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim iRow As Integer = GridView1.GetSelectedRows(0)

    Dim lines() As String = IO.File.ReadAllLines("C:\Data.csv")

    lines(iRow) = lines(iRow).Replace("Value", "OK")

    IO.File.WriteAllLines("C:\Data.csv", lines)

End Sub
  

该进程无法访问该文件' C:\ Data.csv'因为它正被另一个进程使用。

我无法保存更改。

如何将CSV文件搜索到指定字段并进行编辑?

0 个答案:

没有答案