I have a solution containing two projects. One project is just for doing all data stuff and the other one, the startup project, do all the web stuff.

Now I want to get the TasksDataBase.xml from the TaskManagerHelpers class by first getting the projects root directory. But all I get is the TaskManager.Web root directory. (I call the method inside TaskManagerHelpers.cs from a controller inside TaskManager.Web)
How do I get the TaskManager.Data root directory when I’m in a class in the same project?
I’ve tried with theese methodes and similar ones.
- HttpContext.Current.Request.PhysicalApplicationPath;
- System.IO.Path.GetFullPath();
- AppDomain.CurrentDomain.BaseDirectory;
Thanks in advance!
One possibility is to
embed the XML file into the assemblyof the class library and then read it as resource in your web application. Remember that when you publish your web application to a web server all that will get into the package will be the files of this web application. There’s no physical relation to some projects that might have lived into the Visual Studio solution that this web application was part of.You may take a look at the
GetManifestResourceStreammethod which will allow you to read the embedded XML from the referenced assembly.Here’s an example:
Bear in mind though that since the file is embedded into the assembly you will not be able to modify it. It is readonly. If you need to modify it then an alternative approach would consist into copying this file to your web application. For example a good place is the
App_Dataspecial folder. You could even setup a post compilation step that will copy the XML file in this location.And then you can reference it easily:
In this case since the XML file is now physically part of the web application and lives on the hard drive you could also modify it.