Link to home
Start Free TrialLog in
Avatar of Veljean
Veljean

asked on

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.
Avatar of bcolladay
bcolladay
Flag of United States of America image

It seems like you would need to put:

string path = Server.MapPath("Templates\file.dotx");
Avatar of Kyle Abrahams, PMP
In general I like always starting from the root:

eg:

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

the tilda (~) says go to root of the web directory.
Avatar of Veljean
Veljean

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Veljean

ASKER

thanks ged325 , Im not very sure how it works but thanks anyway!!
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.