0

我目前正在用 Visual Basic .Net 编写一个程序。

在程序的一个特定部分中,我试图从文本文件中的特定行读取,更改该文件中的数据值之一(数据值由“,”分隔)并覆盖同一文件中的同一行.

目前,我编写的代码可以完成所有这些工作,但是要使一部分工作,我需要能够确定要指定数据所在的行,然后将其添加到变量中,以便可以使用该行稍后再写。这是我正在谈论的代码:

Private Sub AddC1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddC1.Click
    Dim AddC1 As String = 0
    Dim checkfile As New System.IO.StreamReader("..\..\checkfile.txt")
    Dim check As String = checkfile.ReadToEnd
    Dim objreader As New System.IO.StreamReader("..\..\usernamepassword.txt")
    Dim contents As String = objreader.ReadLine
    For i As Integer = 1 To check
        contents = objreader.ReadLine
        Dim data As New List(Of String)(contents.Split(","))
        If forname.Text = data(2) Then
            If surname.Text = data(3) Then
                AddC1 = data(8) + 1
                Exit For
            End If
        End If
    Next i
    Dim Lines() As String = System.IO.File.ReadAllLines("..\..\usernamepassword.txt")
    Lines(1) = ""
    System.IO.File.WriteAllLines("..\..\username.txt", Lines)
End Sub

请注意,此代码尚未完成。我现在这个解释并不完全有意义,但是我尽我所能去解释。

在此先感谢,阿尔菲。

4

1 回答 1

1
 Dim check As String = checkfile.ReadToEnd

应该

Dim check = File.ReadAllLines("..\..\checkfile.txt").Length

这将为您提供文件中的行数。否则,

For i As Integer = 1 To check

会给你一个错误。不能从整数变为字符串。您可以在内部 if 循环中声明另一个变量并将其设置为 i - 1(因为您的 for 循环从 1 而不是 0 开始),这将为您提供索引的值以供以后使用。

于 2017-03-14T15:19:12.247 回答