Jonathan Dyle
Jonathan Dyle

Reputation: 73

C# Loading a xml file from the current directory?

I use the line below in my C# winform app, this works great but occasionally if the program is being run from the command line I get an error that the config.xml file cannot be found. This is because the 'working directory' is different (I think), I need to say "load config.xml from current directory", how would I do this?

docXML.Load("config.xml");

Thanks Jonathan

Upvotes: 5

Views: 10304

Answers (2)

Jay
Jay

Reputation: 1309

    string Path = "";
    string Filename = ConfigurationManager.AppSettings("Filename");

for loading from current directory

   Path = System.Web.HttpContext.Current.Server.MapPath(Filename);   

for loading from Base directory

    Path = AppDomain.CurrentDomain.BaseDirectory + Filename;        

Upvotes: 3

Catalin DICU
Catalin DICU

Reputation: 4638

string fileName = Path.Combine(Application.StartupPath, "config.xml");

Upvotes: 8

Related Questions