How to get to the website root in ASP.Net

Hi guys,

This one is for ASP.Net developers...or anyone who can help me.

I have a folder right in the root of my website:

C:\Inetpub\vhosts\mydomain.com\MyFolder

I will have a template file in there which my app will access, modify and save later on.

How can I build a path so my app can reach the file regardless of the relative path of the class accessing it? I'm trying to do it taking the website root as starting point and then moving to the actual folder.

I've tried: "~/MyFolder" but it goes to: 'c:\windows\system32\inetsrv\~\MyFolder\Source.html'.

Thanks.
 
Hi Stephen,

It goes just one step back:

'c:\windows\system32\MyFolder\Source.html'.

Its weird the way it works in the VPS because normally "~/" references the root folder of a website.

1000 Thanks for answering
 
are you serving pages out of system32 folder?

i'd HIGHLY recommend making a new domain with pages server out of c:\domains\domain.com or something like this, it will be a lot cleaner for permissions and all.
 
No Stephen, I'm not!!! It just returns that path for some reason. I found the solution bellow:

Just as a reference for anyone having this issue in the future. Use: Server.MapPath("~/MyFolder")

Thanks!
 
Hey,

From your example assuming you have your web site root mapped to
C:\Inetpub\vhosts\mydomain.com

and a folder MyFolder below it

the Myfolder will always live at /MyFolder ie the root of the web site.
ie http://localhost/MyFolder/Source.htm

Within your aps.net code you can use Server.MapPath to turn the
virtual path (/MyFolder/Source.htm) into the physical path ie
C:\Inetpub\vhosts\mydomain.com\MyFolder\Source.htm

eg. Server.MapPath("/SomeDir/SomePage.asp")

Does that help?

Cheers,

Paul.
 
Back
Top