Search code examples
visual-studiovisual-studio-2019csproj

Is there a way to configure project paths outside of the solution file in Visual Studio 2019?


I have a Visual Studio solution with the project I am working on (main project). The solution references a different project that main project requires as a dependency (dependency project).

The solution and the main project are located in one directory:

/mycode/main_project/main_project.sln
/mycode/main_project/main_project.csproj

The dependency project is located in a different directory:

/mycode/dependency_project/dependency_project.csproj

Both of these projects are under source control. I want to create some kind of configuration file for main project that is not under source control, so that another developer could clone both projects wherever they want and simply edit a non-source-controlled configuration file to allow the main project's solution to locate dependency project.

Currently, main solution locates dependency project using a relative path:

../dependency_project/dependency_project.csproj

If I enforce that all developers should clone these two projects to the same directory, the main solution will successfully link to the dependency project and everything will be happy. However, I would prefer that another developer can place the dependency project wherever they want.

Does Visual Studio 2019 Community support any kind of solution configuration file which could be kept ignored by version control and used to resolve the path to the dependency project?


Solution

  • Solution files are very primitive and do not offer such a dynamic functionality. However, if you don't need dependency_project to show up in the IDE, you can still reference it from main_project.csproj and that does give you more options. It should still build fine even if the project doesn't show up in Solution Explorer.

    For example, you could reference it through an environment variable, with a default expected path if that variable isn't set:

    <PropertyGroup>
      <DependencyProjectPath Condition=" '$(DependencyProjectPath)' == ''>../dependency_project/dependency_project.csproj</DependencyProjectPath>
    </PropertyGroup>
    <ItemGroup>
      <ProjectReference Include="$(DependencyProjectPath)" />
    </ItemGroup>
    

    If you set the DependencyProjectPath environment variable before opening the solution (because VS inherits the environment variables from where it was launched), it will override the default setting here (based on the Condition attribute).