Counting a Phrase in a Text File
This function helps to count number of occurences of a phrase in a text file, it will take 2 parameters file name and phrase to search
Function
public int CountPhraseInFile(string strFileName, string strFindText)
{
int retValue = 0;
StreamReader sr;
try
{
sr = File.OpenText(strFileName);
string strText = sr.ReadToEnd();
string strReplace = strText.Replace(strFindText, "");
int count = (strText.Length - strReplace.Length) / strFindText.Length;
retValue = count;
sr.Close();
sr.Dispose();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
return retValue;
}
To Test the function
Response.Write(CountPhraseInFile(@"C:\downloads\test.txt", "type").ToString());

Hi Mr.satish,
Very usefull info..
Thanks for sharing with all of us.
regards,
harshitha