• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 583
  • Last Modified:

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("no-reply@myisphost.net");
            //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("xxx.xxx.xxxxxxx.xxxx");
        protected void btnSend_Click(object sender, EventArgs e)

Open in new window

  • 2
2 Solutions
-Dman100-Author 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?
you want to use the Server.MapPath in a static context try this:

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

Refer to:



Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now