map path to file in website

Posted on 2009-02-13
Last Modified: 2013-11-26
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

Question by:-Dman100-
    LVL 13

    Accepted Solution


    Author Comment

    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?
    LVL 13

    Assisted Solution

    you want to use the Server.MapPath in a static context try this:

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

    Refer to:


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now