vb.net 如何使用 Visual Basic 2008 Express Edition 打开 .txt 文件并将其显示在文本框中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12759151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 16:20:01  来源:igfitidea点击:

How to open a .txt file and display it in a text box using Visual Basic 2008 Express Edition

vb.netc#-to-vb.net

提问by user1724956

I am using Visual Basic 2008 Express Edition, and this is very new to me. I have a browse button, and I added code in such a way that a file is browsed and its path is displayed in a label box when I click the browse button.

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

Similarly I want the content of the file I select to be displayed in text box. I use the following code:

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

    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

But I got syntax a error for the Importsline and StreamReaderas undeclared. How can I fix this problem?

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

回答by T. Fabre

Your import statement should be at the very top of the file, outside of a Sub or Function, and your oReader declaration should be at least inside the class, or inside a method.

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

Additionally, your import is not right. "Imports System.IO.StreamReader" should be "Imports System.IO", otherwise you will only have access to the classes declared within StreamReader (if there are any). What you really want is to import the System.IO namespace so that you have access to the types declared in that namespace.

此外,您的导入不正确。“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

回答by Cdeez

You should be importing System.IOnamespace.

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

And see these changes in the code:

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

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

回答by Ghanshyamsinh Parmar

Its very simple use below code that will help you

它非常简单的使用下面的代码可以帮助你

Open file code

打开文件代码

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

 End If

Save code

保存代码

 If SaveFileDialog1.ShowDialog = DialogResult.OK Then

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

End If

回答by Juman

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()