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

16,349

Solution 1

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.

Solution 2

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.

Solution 3

Right-click the XML file and select properties, then change the Copy to Output Director to one of the other settings than "Do Not Copy". That will place the file into your \bin\ folder alongside the other project output. You can then use AppDomain.CurrentDomain.BaseDirectory as your base path

Solution 4

IF you are running a web project, all the referenced dll files are copied to the bin directory (unless they are in the GAC) and used from there, no matter if you add a reference to another project, Visual Studio first compile it and then copies it to the bin folder of the web project. You can mark your xml file as Content (Compilation Action) and with the copy always option so it always copy it to the bin directory .... the problem is that it sometime look for this files outside of the bin folder but I think that you can handle this.

Share:
16,349
Andreas
Author by

Andreas

System/Web developer. MCP and EPiServer Certified Professional

Updated on July 09, 2022

Comments

  • Andreas
    Andreas almost 2 years

    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!

  • Andreas
    Andreas over 11 years
    If I understand correctly you say that if I want a physical file like I have here i'ts best to put in the startup project in somewhere but I can work around by reading it as a resource?
  • Devin Burke
    Devin Burke over 11 years
    The file won't exist on your hard drive; it will be part of the data layer dll file.
  • Darin Dimitrov
    Darin Dimitrov over 11 years
    Yes, you have the choice: either copy physically the file for example in the App_Data folder or embed it into the assembly.
  • Andreas
    Andreas over 11 years
    Thanks for your answer. My solution is to move it to my start-up project :) Really good explanation!
  • Devin Burke
    Devin Burke over 11 years
    ...having problems with the idea of ever hard coding a path into an application as opposed to putting it in a .config.
  • James Curran
    James Curran over 11 years
    Adding it to a Web.config would work too. The key point is that it'll be impossible to programmatically determine where it would be.