protected void btnParse_Click(object sender, EventArgs e) { object app; object doc; string docFileName; string docPath; string contents; //Cursor.Current = Cursors.WaitCursor txtResults.Text = ""; lblMsg.Text=""; //validate file name docFileName = Server.MapPath(fuFile.FileName); if(docFileName.Length==0) { lblMsg.Text="Please enter a file name"; } //if no path use APP_BASE docPath = Server.MapPath(fuFile.FileName) ;//Path.GetFullPath(docFileName); if(docPath.Length == 0) { //docFileName = Application.StartupPath & "\" & docFileName; } //extract contents of file contents = ""; if(Path.GetExtension(docFileName).ToLower()==".txt") { StreamReader fs; fs=new StreamReader(docFileName); try { contents=fs.ReadToEnd(); } catch { lblMsg.Text="Unable to read from text input file"; contents=""; } } else { try { app = Activator.CreateInstance(Type.GetTypeFromProgID("Word.Application")); } catch { lblMsg.Text="Unable to start Word"; } try { doc = Server.MapPath(fuFile.FileName);//OpenFile(docFileName); } catch { lblMsg.Text="Unable to load document in Word"; } //contents = fuFile.PostedFile.InputStream.Read().ToString();//doc.Content.Text; } //search for email addresses string emails; string email; StringBuilder results=new StringBuilder(); emails = ExtractEmailAddressesFromString(contents); foreach(char email1 in emails) { results.Append(email1 + Environment.NewLine); } //display results lblMsg.Text=String.Format("{0} match(es) found.", emails.Length); txtResults.Text = results.ToString(); } private string ExtractEmailAddressesFromString(string source) { MatchCollection mc; int i; //expression garnered from www.regexlib.com - thanks guys! mc = Regex.Matches(source, @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); string results; results=(mc.Count - 1).ToString(); for (i = 0; i <= results.Length - 1; i++) { results[i]=mc[i].Value; } return results; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)