Click here to Skip to main content
15,894,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I want to search a text (can be name, email, phone number) from a file.

I have a File Upload control and a Button on my ASP.NET page. User may select a file of any format say Word,PDF,text,rtf. Irrespective of the file format, I want to search and fetch those details from the file. i.e, as and when user clicks on the button, I am supposed to perform the search operation and display those details, may be on a grid view. How can I achieve this using C#...?

Thanks and regards
Karthik Harve
Posted
Comments
Prerak Patel 25-Aug-11 5:41am    
No attempts?! What have you tried?
Karthik Harve 25-Aug-11 7:18am    
its not like i have not tried. i can write code to search the data with individual formats. but i want a unique code which can fetch data from file of any format. that code should not have any file format restrictions.
Ed Nutting 25-Aug-11 5:43am    
A few suggested pages:
Uploading Files in C# - MSDN
Regular Expression - MSDN - use this for searching for phone numbers etc.

Hope this helps,

Ed :)

You can search text using regular expression in case of text and rtf files just open and read file in text mode but other file types can be opened in binary mode so you can't search text by this method. other possible solutions are you should open file according to file format i.e use pdf library to search in pdf files, use oledb to search text in word,excel like file formats.
 
Share this answer
 
(search only in doc file)
hope my code will help u
C#
Public string url;
protected void Button3_Click(object sender, EventArgs e)
{
    Process[] ap = Process.GetProcesses();
    foreach (Process p in ap)
    {
        if (p.ProcessName == "WINWORD")
        {
            p.Kill();
        }
    }
    StringBuilder sb = new StringBuilder();
    StringBuilder flenme = new StringBuilder();
    ArrayList li = new ArrayList();
    string pat = @"~\uploads";
    string[] filename = Directory.GetFiles(Server.MapPath(pat));
    foreach (var r in filename)
    {
        string[] split = Regex.Split(r, @"\\");
        li.Add(split [split .Length - 1]);
    }
    foreach (var j in li)
    {
        object file = Server.MapPath(pat) + "\\" + j.ToString();
        object nullobj = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word.Application wordapp = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document doc = wordapp.Documents.Open(ref file, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);
        doc.ActiveWindow.Selection.WholeStory();
        doc.ActiveWindow.Selection.Copy();
        string result1 = doc.Content.Text.Trim();
        string result2 = Regex.Replace(result1, @"[\r\a]", " ");
        string result = Regex.Replace(result2, @"\s+", " ");
        char[] del = { ' ' };
        string[] SplittedResult = result.Split(del);
        doc.Close();
        url = "Default3.aspx?" + TextBox1.Text;
        foreach (string i in SplittedResult)
        {
            if (TextBox1.Text.ToLower() == i.ToLower())
            {
                sb.AppendLine("<span filename="" + j.ToString() + "" style="color: #FF0000">" + j.ToString() + "             </span><br />" + result + "<hr />");
                sb.Append("?//_\\??");
                break;
            }
        }
    }
    Session["Result"] = sb;
    if (Session["Result"].ToString() != "" && Session["Result"] != null)
    {
        Response.Write("<script language=javascript>window.open('" + url + "')</script>");
        //Response.Redirect(uri);
    }
    else
    {
        Literal1.Text = "No match Found";
    }
}

on search result page
C#
protected void Page_Load(object sender, EventArgs e)
  {
      if (Session["Result"] != null)
      {
          string sessionResult = Session["Result"].ToString();
          string FormatedResult = Regex.Replace(sessionResult , @"[\r\n]", " ");
          string FinalResult= FormatedResult .Replace("?//_\\??", "<br />");
          Response.Write(FinalResult);

      }
  }
 
Share this answer
 
v2
Comments
fjdiewornncalwe 10-May-12 15:23pm    
Please don't reopen old questions like this. It is unlikely that the OP is still watching it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900