map path to file in website

I am testing a simple method to send an attachment in an email.  I created a website on my localhost and have included a sample txt file in the root directory of the site.

In my page, I'm calling the following method shown in the code section below.

When I run my page, I get the following error:

Could not find file 'C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\TestAttachement.txt'.

The file is not located in the above directory.

I thought that if the file is found in the website, I didn't need to use an explicit file path.  I guess I do.

How can I map the path to my file?

I was reading some articles that mentioned using Server.MapPath.  However, when I tried this, the intellisense was not finding the Server object and MapPath method.

I also tried HttpServerUtility namespace, but the MapPath member was not available.

I'm using VS2008.

Thanks for any help.

static void AttachmentFromFile()
            //create the mail message
            MailMessage mail = new MailMessage();
            //set the addresses
            mail.From = new MailAddress("");
            //set the content
            mail.Subject = "This is an email";
            mail.Body = "this content is in the body";
            //add an attachment from the filesystem
            mail.Attachments.Add(new Attachment("TestAttachement.txt"));
            //send the message
            SmtpClient smtp = new SmtpClient("");
        protected void btnSend_Click(object sender, EventArgs e)

Open in new window

-Dman100-Software ConsultantAsked:
Who is Participating?
-Dman100-Software ConsultantAuthor Commented:
Hi Ghost,

Thanks for replying to my post.  Those were the exact articles I was reading.

I figured out what the problem was.  My AttachmentFromFile() method was static.

Why can't you use Server.MapPath in a static method?
copyPasteGhostConnect With a Mentor Commented:
you want to use the Server.MapPath in a static context try this:

string s = HttpContext.Current.Server.MapPath("file.XXX");

Refer to:

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.