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.
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.
In general I like always starting from the root:
eg:
Server.MapPath("~\<dir>");
the tilda (~) says go to root of the web directory.
eg:
Server.MapPath("~\<dir>");
the tilda (~) says go to root of the web directory.
ASKER
I dont get it... it gives me a extra folder:
C:\Documents and Settings\DIGEVCE\Mis documentos\Visual Studio 2010\WebSites\ModuloProgra macion\Formularios\Plantillas\CCE_42_Gen_201 1_1.dotx
when physically my file is in
C:\Documents and Settings\DIGEVCE\Mis documentos\Visual Studio 2010\WebSites\ModuloProgra macion\Pla ntillas\CC E_42_Gen_2 011_1.dotx
C:\Documents and Settings\DIGEVCE\Mis documentos\Visual Studio 2010\WebSites\ModuloProgra
when physically my file is in
C:\Documents and Settings\DIGEVCE\Mis documentos\Visual Studio 2010\WebSites\ModuloProgra
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.a spx
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\F ile.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\AppRoo t"
then from there you can just apply the directories as needed.
eg:
C:\inetpub\wwwroot\AppRoot
by default yourfile.aspx looks in otherDIR and any folders underneath it.
So if you had
c:\inetpub\wwwroot\AppRoot
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
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\AppRoo
then from there you can just apply the directories as needed.
string path = Server.MapPath("Templates\