How to get actual path to a file within a virtual directory

R

Roy Chastain

I have need to access a file from my Global.asax. The file is in the same virtual directory as the global.asax. In old .asp I
would use the mappath method. What do I need to do to get the full path so that I can access the file?

Thanks
 
K

Kevin Spencer

Hi Roy,

Believe it or not, you would still use Server.MapPath(url). The Server
object is part of the Page object's HttpContext. And it still works the same
way. :)

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

Roy Chastain said:
I have need to access a file from my Global.asax. The file is in the same
virtual directory as the global.asax. In old .asp I
 
R

Roy Chastain

I can not find a Page object in .net.
I tired this but it does not work

HttpServerUtility the_server = new HttpApplication().Server;
RemotingConfiguration.Configure(the_server.MapPath("web.config"));


Here is the error
Line 5: {
Line 6: HttpServerUtility the_server = new HttpApplication().Server;
Line 7: RemotingConfiguration.Configure(the_server.MapPath("web.config"));


Source File: E:\Inetpub\wwwroot\HGSConfigObjects\global.asax Line: 7

Stack Trace:


[HttpException (0x80004005): Server operation is not available in this context.]
System.Web.HttpServerUtility.MapPath(String path) +115
ASP.Global_asax.Application_OnStart() in E:\Inetpub\wwwroot\HGSConfigObjects\global.asax:7
 
K

Kevin Spencer

The Server is part of the current HttpContext, and is a member of the
HttpApplication class. So, if you're writing for the Application_OnStart
event handler, you would simply refer to it as "Server"

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.


Roy Chastain said:
I can not find a Page object in .net.
I tired this but it does not work

HttpServerUtility the_server = new HttpApplication().Server;
RemotingConfiguration.Configure(the_server.MapPath("web.config"));


Here is the error
Line 5: {
Line 6: HttpServerUtility the_server = new HttpApplication().Server;
Line 7: RemotingConfiguration.Configure(the_server.MapPath("web.config"));


Source File: E:\Inetpub\wwwroot\HGSConfigObjects\global.asax Line: 7

Stack Trace:


[HttpException (0x80004005): Server operation is not available in this context.]
System.Web.HttpServerUtility.MapPath(String path) +115
ASP.Global_asax.Application_OnStart() in E:\Inetpub\wwwroot\HGSConfigObjects\global.asax:7
 
R

Roy Chastain

That worked. Thanks.

The Server is part of the current HttpContext, and is a member of the
HttpApplication class. So, if you're writing for the Application_OnStart
event handler, you would simply refer to it as "Server"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top