Tuesday, March 13, 2012

Server.MapPath error

I am using VS2002, at home and can't seem to get Server.MapPath to work. I
get the error:

The type or namespace name 'Server' could not be found (are you missing a
using directive or an assembly reference?)

My code page is essentially:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

...

public virtual void save()
{
FileStream s = new

// Change the file path below to the appropriate dir:
FileStream(Server.MapPath("profile.bin"),
FileMode.Create, FileAccess.Write);

BinaryFormatter b = new BinaryFormatter();
b.Serialize(s, this);
s.Close();
}

What am I missing from here that would cause the Server object not to be
recognized. I don't have this much trouble with the VS2003 that I have at
work.

Is there something different that would make it work in 2003 and not in
2002?

Thanks,

TomHi,

My guess is that you are not inside of a class that derives from the
Page class (i.e., Server is not visible). Anyway, try the following:

HttpServerUtility myServer = HttpContext.Current.Server;

Note that this has nothing to do with VS 2002 or VS 2003.

Thanks,

Sayed Y. Hashimi

http://www.sayedhashimi.com
Shameless Book Plug: Service-Oriented Smart Clients with .NET 2.0
http://www.amazon.com/exec/obidos/t...=glance&s=books

0 comments:

Post a Comment