6

私のasp.netアプリケーションでは、utilクラスがxmlファイルからいくつかのデータを読み取り、後でこのクラスを呼び出すことができます。ファイルは一度ロードする必要があるため、静的コンストラクターを使用します。

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.
  }
}

「Server.mappath(xxx)」の使用を提案する人もいるかもしれません

ただし、このクラスはxx.aspx.csではありません。したがって、コンテキストには「HttpRequest」または「HttpServerUtilly」はありません。

何か案は?

4

2 に答える 2

13

を使用し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); 
    }
}
于 2011-08-06T09:26:17.710 に答える
4

試す

var path = Path.Combine(
    HostingEnvironment.ApplicationPhysicalPath, 
    "App_Code\\a.xml"
);

http://msdn.microsoft.com/en-us/library/system.web.hosting.hostingenvironment.applicationphysicalpath.aspx

于 2011-08-06T09:34:44.073 に答える