Using relative path for "Start external program" in VS.NET 2010

I know this is a little late to the party, but here's how we do it. The key to this is to set the 'OutputPath' explicitly to the Build directory. This re-bases it to working directory and not the VS install directory.

  1. Update output path for the project to be:
    <OutputPath>$(MSBuildProjectDirectory)\bin\</OutputPath>

  2. Update StartProgram for the project to be:
    <StartProgram>$(OutputPath)Relative.exe</StartProgram>

Here is a sample configuration PropertyGroup:

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == '0-Local|AnyCPU'">
   <!-- default values you should already have in your csproj -->
   <PlatformTarget>AnyCPU</PlatformTarget>
   <DebugSymbols>true</DebugSymbols>
   <DebugType>full</DebugType>
   <DefineConstants>DEBUG;TRACE</DefineConstants>
   <ErrorReport>prompt</ErrorReport>

   <!-- actual output path and start action definition -->
   <OutputPath>$(MSBuildProjectDirectory)\bin\</OutputPath>
   <StartAction>Program</StartAction>
   <StartProgram>$(OutputPath)NServiceBus.Host.exe</StartProgram>
   <StartArguments>NServiceBus.Integration</StartArguments>
</PropertyGroup>

Found the answer here.

In the event that the above link goes dead, the summarized answer is as follows:

  1. Macros don't work here, so forget about that.
  2. Environment variables don't work either, so forget about that as well.
  3. It turns out that Visual Studio.NET (at least 2008 and 2010) uses one of two paths as the base for any relative path specified in the Start external program setting...

If Visual Studio.NET was launched by clicking on the SLN file in Explorer, the base path will be the folder (including the "\") where the SLN resides. Once I modified my relative path to account this and then launched VS.NET 2010 by double-clicking the SLN file, my external program correctly launched when hitting F5.

If Visual Studio.NET was launched from the shortcut on the Start Menu and then the SLN was opened from within Visual Studio.NET, the base path will be [Visual Studio install path]\Microsoft Visual Studio ["9.0" or "10.0" depending on whether using VS.NET 2008 or 2010]\Common7\IDE\.

I guess it makes sense now, but it still kinda stinks that VS.NET will only find my external program correctly depending on how I launch VS.NET.


Similar to what Yobi21 suggested, editing the project file and adding these lines to the main <PropertyGroup> in the project file worked for me:

<StartAction>Program</StartAction>
<StartProgram>$(MSBuildProjectDirectory)\Path\Relative\To\CSProj\Folder</StartProgram>
<StartArguments>Any Required Arguments</StartArguments>

Watch out for the properties in the .csproj.user file overriding those in your regular project file. Note: Starting with Visual Studio 2017, these properties are contained in the launchsettings.json file.

This one stumped me until I deleted the entries.