Get full file path c# web app

Hi
I have a dotx file un my Web PAge, I created a folder called "Templates" (all inside my Vs project)  and I added this file inside.

I would like to get the full path with code in order to dont worry about paths when I publish the page.

I have tried code like

string path = Server.MapPath("file.dotx");
System.IO.FileInfo file = new System.IO.FileInfo(path);

but it doesn work because it gives me a wrong folder.
LVL 1
VeljeanAsked:
Who is Participating?
 
Kyle AbrahamsConnect With a Mentor Senior .Net DeveloperCommented:
if your page is in Formularios then you would need to use the ~ to get back up to the root.

try:

 Server.MapPath("~\Templates\file.dotx");
0
 
bcolladayCommented:
It seems like you would need to put:

string path = Server.MapPath("Templates\file.dotx");
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
In general I like always starting from the root:

eg:

Server.MapPath("~\<dir>");

the tilda (~) says go to root of the web directory.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
VeljeanAuthor Commented:
I dont get it... it gives me a extra folder:

C:\Documents and Settings\DIGEVCE\Mis documentos\Visual Studio 2010\WebSites\ModuloProgramacion\Formularios\Plantillas\CCE_42_Gen_2011_1.dotx

when physically my file is in

C:\Documents and Settings\DIGEVCE\Mis documentos\Visual Studio 2010\WebSites\ModuloProgramacion\Plantillas\CCE_42_Gen_2011_1.dotx
0
 
VeljeanAuthor Commented:
thanks ged325 , Im not very sure how it works but thanks anyway!!
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Your file was most likely in a different directory.

eg:

C:\inetpub\wwwroot\AppRoot\Otherdir\YourFile.aspx

by default yourfile.aspx looks in otherDIR and any folders underneath it.

So if you had
c:\inetpub\wwwroot\AppRoot\otherdir\childDir

yourfile.Aspx could access it via Server.MapPath(childDir + "\" + <file>)

However in your case you wanted to go one level up:

eg:  
c:\inetpub\wwwroot\AppRoot\SomeDir\File.dotx

Correct server.map path is ("..\SomeDir\File.dotx")

But if you change the directory structure then you have to worry about all the links.  The best way to do this is using the tilda:  "~"

tilda means go to AppRoot.

so in this sense "~" == "c:\inetpub\wwwroot\AppRoot"

then from there you can just apply the directories as needed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.