Sunday, 25 January 2015

Read URL file content and get search word count in C#



using System.Text;
using System.IO;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Net;

protected void btnSerach_Click(object sender, EventArgs e)
{
string URIFile = string.Empty;

       URIFile = "http://ravikumargupta.com/App_Log/App_Log_20141113_03_AM.txt";

       LblCount.Text = this.URIFileWordCount("Exception", URIFile).ToString();
}

private int URIFileWordCount(string searchedWord, string url)
{
int RetVal = 0;           
string FileText;

       try
       {
              using (WebClient client = new WebClient())
              {
                    FileText = client.DownloadString(url);
              }

              Regex RExp = new Regex(searchedWord, RegexOptions.Multiline);
              MatchCollection MatchedSearch = RExp.Matches(FileText);

              RetVal = MatchedSearch.Count;
       }
       catch (Exception)
       {  
       }

       return RetVal;
}


No comments:

Post a Comment