Reading from a Text File In Visual Studio C#

Create a new Windows Application

Add a New form

Add a textbox to the form; use the default name for the textbox.

Make sure to change the Multiline Property of the textbox to True.

After changing the Multiline Property of the textbox to True, make the textbox larger in order to fill the form, but leave enough room at the top to add a button.

Add a button to the form above the textbox.

Rename the button to pbSelectFile.

Add an OpenFileDialog to the form.

Rename the OpenFileDialog fDiag1.

Add this “Text Files|*.txt” to the Filter Property of fDiag1.

Copy and paste the code below

private void pbSelectFile_Click(object sender, EventArgs e)
        {
            if (fDiag1.ShowDialog() != DialogResult.Cancel)
            {
                //Set the Text to an empty string in order to reset the text if you
                //have previously opened a file.
                textBox1.Text = "";
                String sLine = "";
                try
                {   
                    //Pass the file you selected with the OpenFileDialog control to
                    //the StreamReader Constructor.
                    StreamReader FileStream = new StreamReader(fDiag1.FileName);                 
                    //Set the string variable to the first line of text in the text file.
                    sLine = FileStream.ReadLine();

                    while (sLine != null)
                    {                        
                        //Add the line of text from the text file to the TextBox, 
                        //and add a carriage return (\r) and line feed (\n) to the end of the
                        //line of text.
                        textBox1.Text = textBox1.Text + sLine + "\r\n";                        
                        //Read the next line of the text file.
                        sLine = FileStream.ReadLine();
                    }
                    //Close the selected text file.
                    FileStream.Close();                    
                }
                catch (Exception err)
                {
                    //Display any errors in a Message Box.
                    System.Windows.Forms.MessageBox.Show("Error:  " + err.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);                     
                }

            } 
     }

}

_____________________

Make sure to add pbSelectFile_Click to the Click Event of the pbSelectFile button.

_____________________

Visual Basic Equivalent

Private Sub pbSelectFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbSelectFile.Click
        Dim FileStream As System.IO.StreamReader

        If fDiag1.ShowDialog() <> Windows.Forms.DialogResult.Cancel Then
            'Set the Text to an empty string in order to reset the text if you
            'have previously opened a file.
            textBox1.Text = ""
            Dim sLine As String = ""

            Try
                'Pass the file you selected with the OpenFileDialog control to
                'the StreamReader Constructor.
                FileStream = New System.IO.StreamReader(fDiag1.FileName)
                'Set the string variable to the first line of text in the text file.
                sLine = FileStream.ReadLine
                While sLine <> Nothing
                    'Add the line of text from the text file to the TextBox, 
                    'and add a carriage return (\r) and line feed (\n) to the end of the
                    'line of text.
                    textBox1.Text = sLine
                    'Read the next line of the text file.
                    sLine = FileStream.ReadLine()
                End While
                'Close the selected text file.
                FileStream.Close()
            Catch Err As Exception
                'Display any errors in a Message Box.
                System.Windows.Forms.MessageBox.Show("Error:  " + Err.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If
    End Sub
End Sub

Advertisements
About

Over 20 years programming experience 5 years Oracle SQL/Stored Procedure programming Over 10 years SQL 2000/2005/2008 Server SQL/Stored Procedure programming Over 18 years Visual Basic 3.0/4.0/5.0/6.0/.Net/2003/2005/2008/2010/2015 Over 10 years ASP.Net Over 7 years Visual C# Over 20 years Microsoft Office and VBA HTML JAVA COBOL EASYTRIEVE FORTRAN

Tagged with: , , , , , ,
Posted in Visual Studio Code Examples
One comment on “Reading from a Text File In Visual Studio C#
  1. news says:

    Awesome post . Thank you for, posting on this blog page dude. I shall email you again! I did not know that.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: