In my asp.net application,I have a util class will read some data from a xml file,then I can call this class later,the file should loaded once,so I use the static constructor.
class UtilHelper{
static UtilHelper(){
XmlDocument doc=new XmlDocument();
doc.load("a.xml"); //here the asp.net cannot find the file,it always try to find file in the iis's dictionary.
}
}
Some people may suggest I use the "Server.mappath(xxx)"
But this class is not the xx.aspx.cs. So there is no "HttpRequest" or "HttpServerUtilly" in the context.
Any ideas?
A relative path refers to a location that is relative to a current directory. Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy.
Absolute and relative paths in script tools You can also set this option by right-clicking the script tool, clicking Properties, then clicking the General tab. At the bottom of the dialog box, check Store relative path names (instead of absolute paths).
An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path. Relative path is defined as the path related to the present working directly(pwd) ...
Answer. A relative path is an address that points to the current directory or project folder. In the example illustrated in this lesson, the relative path is images/mountains. jpg and it assumes we have an images subfolder nested within a project folder.
Use HttpContext.Current.Server.MapPath
.
class UtilHelper
{
static UtilHelper()
{
XmlDocument doc = new XmlDocument();
string fileName = HttpContext.Current.Server.MapPath("~/App_Code/a.xml");
doc.load(fileName);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With