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

  • Thread starter Thread starter Roy Chastain
  • Start date Start date
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
 
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
 
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
 
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
 
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"
 
Back
Top