how to set path to a file in ASP C# properly?

2019-09-15 04:17发布

问题:

working on a website here, in ASP and C#.

my problem is, when I try to read a file, it finds the wrong directory. here are the code im using:

addtext.Text = new WebClient().DownloadString("readme.htm");

error look like this:

Could not find file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\readme.htm'.

how do I guide the path to the same as my default.aspx ??

回答1:

This page has pretty much everything you might need to know about resolving paths in ASP .Net



回答2:

I suggest you to use app keys for saving the address of the folder which contains your files. You can build a hierarchy for different type of files.

<add key="WebResources" value="~/Assets/WebResources/" />
<add key="ImageRoot" value="Images\Web" />

and then access them like this and concatenate them with further folder of hierarchy .

string WebResources = ConfigurationManager.AppSettings["WebResources"];
string dir = System.Web.HttpContext.Current.Server.MapPath(WebResources);