文件I / O功能通常在“Microsoft.VisualBasic”命名空间中可用,但目标平台不支持它

时间:2012-10-09 18:05:05

标签: vb.net visual-studio-2010

我想打开我的.txt文件,但是我收到此错误

错误1未声明“打开”。文件I / O功能通常在“Microsoft.VisualBasic”命名空间中可用,但目标平台不支持它。

我正在使用vb2010,我认为代码错误,因为它适用于vb6。如何将其更改为在vb2010中工作?

Private Sub zapisz_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles zapisz.Click
    Open "C:\Plik.txt" For Append As #1 'zapis
    print #1, "a" & a.Text 
    Print #1, "b" & b.Text
    Print #1, "c" & c.Text
    Print #1, "d" & d.Text
Close #1 
End Sub


Private Sub wczytaj_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles wczytaj.Click
    Dim strText As String, strIndex As String
Open "C:\Plik.txt" For Input As #1
    Do Until EOF(1)
        Input #1, strText
        strIndex = Left(strText, 1)
        strText = Right(strText, Len(strText) - 1)
        Select Case strIndex
            Case "a" : a = strText
            Case "b" : b = strText
            Case "c" : c = strText
            Case "d" : d = strText
        End Select
    Loop
Close #1
End Sub

1 个答案:

答案 0 :(得分:2)

.NET框架中的工具使这种东西比旧的VB6文件访问语句更清晰,更直观:

Private Sub zapisz_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles zapisz.Click
    Using writer As New StreamWriter("C:\Plik.txt", True)
        writer.WriteLine("a" & a.Text)
        writer.WriteLine("b" & b.Text)
        writer.WriteLine("c" & c.Text)
        writer.WriteLine("d" & d.Text)
    End Using
End Sub

Private Sub wczytaj_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles wczytaj.Click
    For Each line As String In File.ReadAllLines("C:\Plik.txt")
        Dim index As String = line.Substring(0, 1)
        Dim text As String = line.Substring(1)
        Select Case index
            Case "a"
                a = text
            Case "b"
                b = text
            Case "c"
                c = text
            Case "d"
                d = text
        End Select
    Next
End Sub