Skip to main content

Read A Text File In Visual Basic

There are many ways to write text or string to a file. In this tutorial, we are going to use StreamReader class.

StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output. Use StreamReader for reading lines of information from a standard text file.


Required Preferences

  • System.Runtime.Extensions.dll for StreamReader class

Procedure

  1. Open the previous project from Write Text To File In Visual Basic Tutorial and rename the project to ReadFileVB or create a new project but it is recommended to use the existing project in the said tutorial.
  2. Once the project is loaded, open the FormMain and add new CommandButton and set the name property to ButtonReadFile and text property to Read File
  3. Double click on to ButtonReadFile to view the code then copy the codes below and paste it to ButtonReadFile_Click event.

  4.  1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    Using OpenFile As New OpenFileDialog

    With OpenFile
    'Create InitialDirectory variable and get the My Documents folder path
    Dim InitialDirectory As String = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)

    'Set the initial directory
    .InitialDirectory = InitialDirectory

    'Set the file extention to filter
    .Filter = "Text files (*.txt)|*.txt;"

    'Set the restore directory
    .RestoreDirectory = True

    If .ShowDialog = DialogResult.OK Then

    Using TextStreamReader As StreamReader = New StreamReader(.FileName)

    TextBoxContainer.Text = TextStreamReader.ReadToEnd()

    TextStreamReader.Close()

    End Using

    Else

    'If Cancel button was click then
    'Do nothing

    End If

    End With

    End Using

  5. Save the project for the future tutorial and run it.
  6. That's all. I hope that you enjoy my tutorials. Subscribe to this page for more.

Comments

Popular posts from this blog

Display Test Object Data In Omicron Control Center Custom Dialog

Welcome to the second part of the tutorial series about  Omicron Control Center (OCC) Custom Dialog. In this tutorial, I will show you how to retrieve the data from the OCC document test object and display it to the custom dialog. Now, open the OCC document that created in the first part of this series. If you don't have it, please check the first part of the series ( Omicron Control Center Custom Dialog - Creating Custom Dialog ). Follow the step by step procedure there to create the a OCC document then return to this page. Before we start writing coding, we have to add Omicron libraries to reference of the existing OCC document. In the View tab and under the Document Views group, click the Script View button to open the scripts we've created in the previous tutorial. Stop the script by clicking the Stop Script button under the Run Script group to enable us to edit or modify the script. Click the References button then the reference dialog will open. In the reference list,...

Processing The Relay Setting Parameters In Omicron Import Filter

Welcome to my page and welcome to the fifth part of Omicron Import Filter tutorial series. In this tutorial, I will show you how to process the relay setting parameters that to be import in the Xrio converter. In this tutorial, we will focus on the Visual Studio program. We're going to modify the codes in our project called MyImportFilter that we've created in the previous tutorial.  Download the project in this link  MyImportFilter - Process Relay Setting Parameters . Extract and open the project. Once the project is loaded, add New Folder inside the project and set the name to SEStudio1. We need this to make organize our project class filter.  SE Studio 1 stands for Schneider Electric Studio 1 which is the software that used in communicating MiCOM relays particularly P12x series. And yes we're going to create a new import filter and user interface form MiCOM P12x series relay. I think this will applied to other MiCOM P series relay. Add a new Form inside ...

Install And Use Newly Created Import Filter In Omicron Test Universe

Welcome to my page. This is the second part of the Omicron Import Filter tutorial series. In this tutorial, I will show you how install  and use the Import Filter that we've created in the previous tutorial in this series using two methods.  The first one via Test Object and the second one is via script. Open the Visual Studio project called MyImportFilter that we created in the first part of tutorial which can be found here ( Omicron Import Filter - Create New Filter ). Once the project opened, right click on the project at the solution explorer the click Open Folder in File Explorer. The project file directory will open. Open the  bin\Debug folder and you will find the two files in that folder ( MyImportFilter.dll and  SampleFilter.xriofilter ). Create new folder in that directory and set the folder name to  MyImportFilter . Move the  MyImportFilter.dll file to the  MyImportFilter folder then, copy the  MyImportFilter folder and the  Sa...