Search code examples
visual-studio-2015msbuildvsixcsprojenvdte

How can I set custom MsBuild property from VSIX without writing into .csproj file?


I create a VSIX extension for Visual Studio, which adds a custom drop-down menu. I need the value from this menu to apply to the .csproj property of the file without changing the file itself, like the configuration menu. For example:

<PropertyGroup>
  <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
  <DefineConstants>DEBUG;TRACE;</DefineConstants>
</PropertyGroup>

-- default content of .csproj. When I selecting "Debug" from Visual Studio configuration drop-down menu, Configuration property value switch to Debug without changing .csproj file and '#if DEBUG'-block in code file is activated.

I have multiply projects in solution and each project have this property. If I overwrite .csproj, then these changes will be monitored by the source control, which I do not want. Help please.


Solution

  • How can I set custom MsBuild property without writing into .csproj file?

    If the custom MSBuild property is a PropertyGroup in the project file, you can invoke MSBuild to pass an parameter into the project file by MSBuild command line:

    <PropertyGroup>
      <CustomTestName>TestValue1</CustomTestName>
    </PropertyGroup>
    

    The command line:

    msbuild /p:CustaomTestName=TestValue2
    

    Alternatively, you can use the option "condition" in the custom property, like [Debug/Release] in project file:

    <PropertyGroup>
      <CustomTestName>TestValue</CustomTestName>
    </PropertyGroup>
    
    <Target Name="CheckPropertiesHaveBeenSet">
      <Error Condition="'$(CustomTestName )'=='CustomTestNameNotSet'" Text="Something has gone wrong.. Custom Test Name not entered"/>
      <PropertyGroup>
        <CustomTestName Condition="'$(CustomTestName)'=='ChangeTestValue'">ChangeTestValue</CustomTestName >
      </PropertyGroup>
    </Target>