Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Can you help me about this issue? This a code for collect email address from txt or html file. but there are some method I can't understand. I convert it from vb to c#.

C#
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;
            
    }
Posted
Comments
[no name] 25-Apr-14 15:15pm    
Are we supposed to guess what it is that you do not understand?
[no name] 25-Apr-14 15:38pm    
And? You think that somehow posting a bunch of ASP.NET code tells me what the single thing about the C# code that you do not understand is? We cannot read your mind to know what it is that you do not understand and if you think that someone is going to write a book for you explaining every single line of code, you are mistaken.
Shahnawazcode 25-Apr-14 15:48pm    
Thanks for your time. I am just almost done. But a single line problem that is shows:
Cannot implicitly convert type 'string[]' to 'string'.<br>at he last line when i try to return it.

MatchCollection mc = null;
int i = 0;

// expression garnered from www.regexlib.com - thanks guys!
mc = Regex.Matches(source, "([a-zA-Z0-9_\\-\\.]+)@([a-zA-Z0-9_\\-\\.]+)\\.([a-zA-Z]{2,5})");
string[] results = new string[mc.Count - 1 + 1];
for (i = 0; i <= results.Length - 1; i++)
{
results[i] = System.Convert.ToString(mc[i].Value);
}

return results;
[no name] 25-Apr-14 15:52pm    
That is because "results" is an array and you have the method set to return a single string. Change the return type on your method to string[] and that should fix it.
Shahnawazcode 25-Apr-14 15:57pm    
when i give return results[i] it compile fine but runtime produce error
System.IndexOutOfRangeException: Index was outside the bounds of the array.

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