Relative path for Reference within imported project


  • I am new to MSBuild and feel I must be missing something obvious.

    I am importing a .csproj file into a .proj in the parent directory.  When I try and build the .proj I get errors about not being able to find files that are references that have a relative HintPath.  An example of the Reference in the .csproj is:

    <Reference Include="Win32Security, Version=3.0.3400.16741, Culture=neutral, PublicKeyToken=f2e3d021a3694e92, processorArchitecture=MSIL">
    <HintPath>..\Support Projects\Win32SecurityV05\DLLs\Win32Security.dll</HintPath>

    If the .proj is in the same directory as the .csproj it works fine.  If I change the HintPaths to be relative to the .proj file then it works from MSBuild but not from Visual Studio.

    What am I missing? 


    Tuesday, May 25, 2010 10:12 PM

All replies

  • Hi Mont,

    All relative paths in imported projects are interpreted relative to the directory of the imported project. Therefore, if a project file is imported into several project files in different locations, the relative paths in the imported project file will be interpreted differently for each imported project. For more information, please see:

    Import Element (MSBuild)

    And I recommend you use $(MSBuildProjectDirectory) in your path, otherwise, Visual Studio maybe not find the path.

    Best Regards,


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, May 27, 2010 2:56 AM
  • Hello Nancy,

    When I go into the .csproj file and edit the HintPath to use $(MSBuildProjectDirectory) and then try and build in Visual Studio (2008) it is unable to resolve the reference to the assembly.

    Do you know how I can get this to work with Visual Studio?


    P.S. To anyone reading this thread or the above referenced documentation page for the Import Element you should note the following:

    In this sentence:

    "All relative paths in imported projects are interpreted relative to the directory of the imported project."

    The second use of "imported" should be "importing".  i.e.

    "All relative paths in imported projects are interpreted relative to the directory of the importing project."

    Not only is this what actually occurs but it also makes sense with the sentence that follows.


    Thursday, May 27, 2010 3:40 PM
  • Nancy,

    Sorry, I just realized why MSBuildProjectDirectory didn't work.  It is the directory of the current project, duh, my brain wasn't firing.

    However that doesn't help.  MSBuild is using the top project's value for MSBuildProjectDirectory, not the value for the sub-project.  His means that using MSBuildProjectDirectory is no better than using a standard relative path, and in fact produces the same result.

    What I need is some way to refer to a path relative to the sub/imported project, not the parent/importing project.  That way it will work both from my .csproj within Visual Studio as well as from any MSBuild .proj that imports that .csproj.



    Thursday, May 27, 2010 3:56 PM
  • OK, I've figured out more about how to deal with imports and MSBuild unfortunately my conclusion is that MSBuild may not work for us (hoping that someone can prove me wrong).

    The property I would like to use is MSBuildThisFileDirectory.  It gives the path relative to the project file it is used in, not the top most project file.  However this is new to MSBuild 4.0 (.NET 4.0) which isn't an option to me at the moment.  Even with this property all hope is lost, which I will explain below.

    Without this 4.0 property what you can do is to define your own property and then override it in the parent.  Ex:

    In .csproj:

      <SupportProjects>..\Support Projects</SupportProjects>
      <Reference Include="SomeDLL, Version=1.0, Culture=neutral, PublicKeyToken=2b02b4623426f73b, processorArchitecture=x86">

    In .proj (located in parent directory to .csproj file):

    <Project xmlns="" ToolsVersion="3.5">
    	<Import Project="C:\MyTrunk\MyLibrary\MyLibrary.csproj" />
    		<SupportProjects>Support Projects</SupportProjects>

    This will give references that work both in Visual Studio and in MSBuild.  However, this turns out to be useless because every single references, to every file (not just assembly references but also code files) in the .csproj must be handled similarly.  That means the every time you add a code file (or any file) you have to manually edit the .csproj to change the path to use your custom property.  Even if you are using MSBuild 4.0 then you still have to change the reference for every file.

    This leads me to believe that people are not including their .csproj files in MSBuild projects.  If I have missed something and there is some way to make this work PLEASE let me know.



    Thursday, May 27, 2010 4:57 PM
  • I always just include the .sln in my .proj, rather than the .csproj's
    Sunday, January 09, 2011 8:42 PM