| Next Tip?
Home » CSharp

How to Read Comments from file using C#

12 July 2009 1,748 views 2 Comments
1 Star2 Stars3 Stars4 Stars5 Stars (3 votes, average: 4.00 out of 5)
Loading ... Loading ...

I have asked the same question from one of my readers with the following scenario:

Sir,

I am creating a console application in which I need to get comments from a text file and display in a group of SingLine Comments and MultiLine Comments.

The following code snippet described the whole solution by-itself:

#region Author initials

/* How to get comments from

* a text file

* Author : Gaurav Arora

* Date : July 12, 2009

* Series: A Step Ahead Series*/

#endregion

#region Namespaces Used

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.IO;

#endregion

namespace ReadingCommetFile.AstepAheadSeries

{

#region Class displaying Comments

class Program

{

static void Main(string[] args)

{

//Set the actual path from your system

const string strFilePath = @”E:\Drive-F\My Articles\ReadingCommetFile\ReadingCommetFile\myComments.txt”;

ReturnComments objReturnComemnts = new ReturnComments();

Console.WriteLine(“Single Line Comments : “);

String strSingleLine = objReturnComemnts.GetCommentsinUpperCase(strFilePath, “SingleLine”);

Console.WriteLine(strSingleLine);

Console.WriteLine(“MultiLine Comments : “);

String strMultileLine = objReturnComemnts.GetCommentsinUpperCase(strFilePath, “MultiLine”);

Console.WriteLine(strMultileLine);

Console.Read();

// Suspend the screen.

Console.ReadLine();

}

}

#endregion

#region Class containing methods to read comments

class ReturnComments

{

public String GetCommentsinUpperCase(String strFilePath, String strCase)

{

int counter = 0;

string line;

string strRetunComments = String.Empty;

StringBuilder sbSingleLine = new StringBuilder();

StringBuilder sbMultiLine = new StringBuilder();

//Create streamReader object

StreamReader file = new StreamReader(strFilePath);

//Read line by line

while ((line = file.ReadLine()) != null)

{

switch (strCase)

{

case “SingleLine”:

if (line.Contains(“//”)) //for singleline comment

{

//Get rest part of line in uppercase and move the counter to next line

String strLine = line.Replace(“//”, “”);

strLine.ToUpper();

sbSingleLine.AppendLine(strLine);

counter++;

}

strRetunComments = sbSingleLine.ToString();

break;

case “MultiLine”:

if (line.Contains(“/*”)) //for multiple line comments

{

String strLine = line.Replace(“/*”, “”);

strLine.ToUpper();

sbMultiLine.AppendLine(strLine);

counter++;

}

if (line.Contains(“*/”)) //Termination of multiple line comments

{

String strLine = line.Replace(“*/”, “”);

strLine.ToUpper();

sbMultiLine.AppendLine(strLine);

counter++;

}

strRetunComments = sbMultiLine.ToString();

break;

default:

//by default line which does not fall in above cases may be treated as a part of multiple comment

//in this case I supposed that this is a part of multiline comment

line.ToUpper();

sbMultiLine.AppendLine(line);

counter++;

break;

}

}

sbSingleLine = null;

sbMultiLine = null;

file.Close();

return strRetunComments;

}

}

#endregion

/* Note : From the help of above you can also get the other details like Author Name, Description etc.

* by Checking cases. */

}

Following is the final output:

Final Output

Final Output

Popularity: 24%

Post to Twitter Tweet This Post

Related posts:

  1. Sorting an array in c# using System; class SortReverse { public static void Main() {...

Related posts brought to you by Yet Another Related Posts Plugin.

Ads by Lake Quincy Media

2 Comments »

  • Gaurav Arora (author) said:

    Testing

    ?
  • Tania said:

    Interesting, I`ll quote it on my site later.
    Have a nice day
    Tania

Leave your response!

You must be logged in to post a comment.

Get Adobe Flash playerPlugin by wpburn.com wordpress themes