0

我正在使用Visual Basic 2008 Express Edition,这对我来说很新。我有一个浏览按钮,我添加代码的方式是当我单击浏览按钮时浏览文件并在标签框中显示其路径。

同样,我希望我选择的文件的内容显示在文本框中。我使用以下代码:

    Imports System.IO.StreamReader
    Dim oReader As StreamReader

    OpenFileDialog1.CheckFileExists = True
    OpenFileDialog1.CheckPathExists = True
    OpenFileDialog1.DefaultExt = "txt"
    OpenFileDialog1.FileName = ""
    OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
    OpenFileDialog1.Multiselect = False

    If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        oReader = New StreamReader(OpenFileDialog1.FileName, True)
        RichTextBox1.Text = oReader.ReadToEnd
    End If

但是我得到了该Imports行的语法错误,并且StreamReader未声明。我该如何解决这个问题?

4

4 回答 4

2

您的 import 语句应该在文件的最顶部,在 Sub 或 Function 之外,并且您的 oReader 声明应该至少在类内部或方法内部。

此外,您的导入不正确。“Imports System.IO.StreamReader”应该是“Imports System.IO”,否则您将只能访问 StreamReader 中声明的类(如果有的话)。您真正想要的是导入 System.IO 命名空间,以便您可以访问该命名空间中声明的类型。

Imports System.IO

Public Class MyForm

    ' ... Whatever code you have for your form

    Public Sub OpenFile()

        Dim oReader As StreamReader

        OpenFileDialog1.CheckFileExists = True
        OpenFileDialog1.CheckPathExists = True
        OpenFileDialog1.DefaultExt = "txt"
        OpenFileDialog1.FileName = ""
        OpenFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
        OpenFileDialog1.Multiselect = False

        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            oReader = New StreamReader(OpenFileDialog1.FileName, True)
            RichTextBox1.Text = oReader.ReadToEnd
        End If

    End Sub

End Class
于 2012-10-06T11:08:48.403 回答
0

您应该导入System.IO命名空间。

并在代码中查看这些更改:

Dim oReader As StreamReader
openFileDialog1.CheckFileExists = True
openFileDialog1.CheckPathExists = True
openFileDialog1.DefaultExt = "txt"
openFileDialog1.FileName = ""
openFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"
openFileDialog1.Multiselect = False
If openFileDialog1.ShowDialog() = DialogResult.OK Then
    oReader = New StreamReader(openFileDialog1.FileName, True)
    richTextBox1.Text = oReader.ReadToEnd()
End If
于 2012-10-06T11:46:01.263 回答
0
Dim reader As StreamReader = My.Computer.FileSystem.OpenTextFileReader("C:\\testfile.txt")

    Dim a As String
    Dim B As String
    Do
        a = reader.ReadLine

        For i = 0 To a.Length

            If a.Substring(i, 1).ToString() = "{" Then

                B = B & a.Substring(i)

                LBL.Text = B
            End If
          'Iam Juman Mandra
        Next
    Loop Until a Is Nothing

    reader.Close()
于 2017-03-14T08:04:36.650 回答
0

下面的代码非常简单,可以帮助您

打开文件代码

  If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            Me.RichTextBox1.LoadFile(OpenFileDialog1.FileName, RichTextBoxStreamType.PlainText)

 End If

保存代码

 If SaveFileDialog1.ShowDialog = DialogResult.OK Then

    RichTextBox1.SaveFile(SaveFileDialog1.FileName, RichTextBoxStreamType.PlainText)

End If
于 2017-02-21T03:57:50.177 回答