Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get absolute path of another project in a solution C#

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.

solution

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!

like image 667
Andreas Avatar asked Jan 25 '13 21:01

Andreas


2 Answers

One possibility is to embed the XML file into the assembly of 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 GetManifestResourceStream method which will allow you to read the embedded XML from the referenced assembly.

Here's an example:

// you could use any type from the assembly here
var assembly = typeof(TaskManagerHelper).Assembly;
using (var stream = assembly.GetManifestResourceStream("TaskManager.Data.DataBase.TasksDataBase.xml"))
using (var xmlReader = XmlReader.Create(stream))
{
    // ... do something with the XML here
}

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_Data special 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:

string xmlFile = HostingEnvironment.MapPath("~/App_Data/TasksDataBase.xml");
using (var xmlReader = XmlReader.Create(xmlFile))
{
    // ... do something with the XML here
}

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.

like image 95
Darin Dimitrov Avatar answered Sep 23 '22 13:09

Darin Dimitrov


Just because the two projects are located in the same folder tree during development, says nothing about where they'll be located at run time. It's entirely possible that that could be on different machines.

"No," you say. They'll will definitely be on the same machine in the same c:\inetpub tree. That may be true, but that's your policy, not a requirement.

If you are going to establish a hard policy about where they are located, then you can hard-code that into you code.

like image 23
James Curran Avatar answered Sep 24 '22 13:09

James Curran