connectseekers.com

Saturday, 14 June 2014

How to Read a Text File Line by Line in C# in Console Application

6/14/2014 - By Pranav Singh 0

In this article I will show you how to read a text file line by line in c# in console application.  


Now for this application first we will create a new console application, add the below code in your console application.

C#.Net
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DemoConsoleApplication
{
    class Program
    {
        /// <summary>
        /// How to Read a Text File Line by Line in C# in Console Application
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            int totallinecounter = 0;
            string textfileline;

            /*Code to Read the file.*/
            StreamReader objstreamreaderfile = new StreamReader(@"D:\DemoTextFile.txt");
            while ((textfileline = objstreamreaderfile.ReadLine()) != null)
            {
                Console.WriteLine(textfileline);
                totallinecounter++;
            }
            objstreamreaderfile.Close();
            System.Console.WriteLine("Total no of lines in text file is  {0}.", totallinecounter);
            System.Console.ReadLine();
        }
    }
}

VB.Net
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.IO

Namespace DemoConsoleApplication
    Class Program
        ''' <summary>
        ''' How to Read a Text File Line by Line in C# in Console Application
        ''' </summary>
        ''' <param name="args"></param>
        Private Shared Sub Main(ByVal args As String())
            Dim totallinecounter As Integer = 0
            Dim textfileline As String

            'Code to Read the file.

            Dim objstreamreaderfile As New StreamReader("D:\DemoTextFile.txt")
            While (InlineAssignHelper(textfileline, objstreamreaderfile.ReadLine())) <> Nothing
                Console.WriteLine(textfileline)
                System.Math.Max(System.Threading.Interlocked.Increment(totallinecounter), totallinecounter - 1)
            End While
            objstreamreaderfile.Close()
            System.Console.WriteLine("Total no of lines in text file is  {0}.", totallinecounter)
            System.Console.ReadLine()
        End Sub
        Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
            target = value
            Return value
        End Function
    End Class
End Namespace

Now we will first create a new text file.


Now we will run the application.

 

DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com
Email Id : contact@aspdotnet-pools.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Top jQuery Plugins

Global Classified : Connectseekers.com
© 2014
back to top