views:

207

answers:

3

Let's say I have a class called AppConfig:

public static class AppConfig
{
    private static XDocument config = 

    public static AppConfig()
    {

    }
}

How do I XDocument.Load the ~App.Config file? Isn't it something like Server.SOMETHING? What namespaces do I need to include>

+1  A: 
XDocument.Load(HttpContext.Current.Server.MapPath("~/web.config"));

is probably what you're looking for. This helper class "Server" lives on the "current" HttpContext inside the System.Web namespace, so add a

using System.Web;

to your code.

Marc

marc_s
no, you can't get Server class in System.web.
Muhammad Akhtar
@Muhammad: you're absolutely right, my oversight - it's part of the HttpContext, of course.
marc_s
+1  A: 

I think what is his problem, he is not able to get server class in his class

Server is property of the Page class that your page inherits from, it not a global. if you are trying to access from a class, use

HttpContext.Current.Server.MapPath("");

and add reference

using System.Web;

OR can be get directly

System.Web.HttpContext.Current.Server.MapPath("");
Muhammad Akhtar
A: 

I'm not sure why you're trying to do that ... if you want to access the values in your web.config or app.config (for client apps) there is already a wrapper class set up to do that named My.Settings.

Those app.config and web.config files are a pain in the keester to deal with directly.

Ron

Ron Savage