Search code examples
c#visual-studiovisual-studio-2017csprojcsproj-user

Use relative paths for working directory & start in C# project


In a C# project I try to use relative paths for "Start external program" and for "Working directory".

enter image description here

I tried relative paths starting with ../ and relative paths with $(SolutionDir) / $(ProjectDir)

With all tries I get an error popup. (The external program cannot be found / the working directory you entered does not exist) - see screenshots.

enter image description here

Is it possible to use relative paths and how? I also searched on msdn but there is almost no info about the csproj.user file.

We need this as we don't like to force a folder structure for all developers.

This is stored in the csproj.user file (myproject.csproj.user) like this:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <ProjectView>ProjectFiles</ProjectView>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'">
    <StartAction>Program</StartAction>
    <StartWorkingDirectory>%24%28SolutionDir%29\..\..\..\..\mydir</StartWorkingDirectory>
    <StartProgram>%24%28SolutionDir%29\..\..\dir\myapplication.exe</StartProgram>
  </PropertyGroup>
</Project>

Solution

  • I tested all the ideas. Unfortunately none of the answers did work or had enough detail, so I added my own.

    • $(ProjectDir), $(MSBuildProjectDirectory) and $(SolutionDir) won't work. Not in the csproj and not in the csproj.user. Also not escaped, quotes etc. This is clear from the error popup when running:

    enter image description here

    • Relative paths does work, but StartProgram and StartWorkingDirectory has different initial directories!

    • StartProgram starts in the directory where the solution file (.sln) is located

    • StartWorkingDirectory starts in the bin directory of the project.

    So I have now working for me:

    • Start Program: ..\mydir\myapplication.exe
    • Start Working Directory: ..\..\..\..\..\mydir (same directory as the myapplication.exe)