Tuesday, 19 June 2012

How to read the text file in C#.NET or VB.NET Windows Forms || StearmReader

Hi friends, in the previous article I explained about How to Create Text File or notepad file using C# and VB.NET.In this article I will explain about how to read the text file in C#.NET or VB.NET.
                         For this we are using StreamReader.StreamReader is a class.It reads the text. It found in System.IO namespace.
Let I explain using the examples.
1. The first program explain how to read the first line in the text file.

VB.NET:
Imports System.IO
Imports System.Text
Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim line As String

        ' Create new StreamReader instance.
        Dim reader As StreamReader = New StreamReader("c:\\temp\cid.txt")
        ' Read one line from file
        line = reader.ReadLine
        ' Write the line we read from "cid.txt"
        MessageBox.Show(line)
    End Sub
End Class


C#.NET:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text;
Public Class Form2
{

     private void Form2_Load(System.Object sender, System.EventArgs e)
     {
         

          // Create new StreamReader instance.
          StreamReader reader = new StreamReader("c:\ temp\cid.txt");
          // Read one line from file
          line = reader.ReadLine();
          // Write the line we read from "cid.txt"
          MessageBox.Show(line);
     }
    Public Form2()
     {
          Load += Form2_Load;
     }
}
The above code will show the first line in the cid.txt as an alert.
2. The second program explain how to read all lines in the text file.
VB.NET:
Imports System.IO
Imports System.Text
Public Class Form2

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim line As String, i As Integer
        For Each line In File.ReadAllLines("c:\temp\column.txt")

            ' Split line on comma
            Dim parts As String() = line.Split(" ")
            Dim swriter As StreamWriter
            swriter = File.AppendText("c:\temp\CID.txt")
            swriter.WriteLine(parts(0))
            swriter.Close()
            i += 1
        Next

    End Sub
End Class
C# Code:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Text;
Public Class Form2
{

     private void Form2_Load(System.Object sender, System.EventArgs e)
     {
          string line = null;
          int i = 0;

          foreach (string line_loopVariable in File.ReadAllLines("c:\temp\column.txt")) {
              line = line_loopVariable;
              // Split line on comma
              string[] parts = line.Split(" ");
              StreamWriter swriter = null;
              swriter = File.AppendText("c:\temp\CID.txt");
              swriter.WriteLine(parts[0]);
              swriter.Close();
              i += 1;
          }

     }
    Public Form2()
     {
          Load += Form2_Load;
     }
}
The above example shows how to read all the lines from the text file.

"If you like my blog or articles, you can appreciate by leaving your comments or Liking my Facebook page Aspdotnet-kishore, following on Google+ Aspdotnet-Kishore, Twitter  on AspdotnetKishore, Linked in Aspdotnet-Kishore, stumbling my posts on stumble upon and subscribing on  RSSfeed Aspdotnet-Kishore for free updates directly to your Email inbox . Watch my blog  for more articles."

No comments:

Post a Comment

© 2012-2014 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.