5

I have solution in visual studio 2010 c#. In the pre build event I execute command that change files in this solution. The updated file is not built in the current build. How can I do that the file, with the changes, will be built in the current build???

Thanks.

user2717436
  • 775
  • 1
  • 10
  • 23

1 Answers1

5

Ok, it seems i figured out your issue.

I have set up a simple Console app and this event:

<PreBuildEvent>copy "D:\Program.cs" "D:\Projects\PreBuildFileModification\FileModification\Program.cs" /Y</PreBuildEvent>

And alas, this does not work! (copy happens and updates file on disk, but Visual Studio does not detect these changes and loads cached file).

Solution is to add:

<UseHostCompilerIfAvailable>false</UseHostCompilerIfAvailable>

In your configuration, eg:

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
  <PlatformTarget>x86</PlatformTarget>
  <DebugType>pdbonly</DebugType>
  <Optimize>true</Optimize>
  <OutputPath>bin\Release\</OutputPath>
  <DefineConstants>TRACE</DefineConstants>
  <ErrorReport>prompt</ErrorReport>
  <WarningLevel>4</WarningLevel>
  <UseHostCompilerIfAvailable>false</UseHostCompilerIfAvailable>
</PropertyGroup>

And now this works like a charm.

Thanks to Rob Butterworth

Aleksei Poliakov
  • 1,322
  • 1
  • 14
  • 27
  • thank you very much!! sorry that I did not mention it before - but my application is a silverlight application. my web.config file does not recognize the "PropertyGroup" tag... Do you have any idea? – user2717436 Dec 02 '13 at 08:40
  • it is not a web.config, it is .csproj file. And PropertyGroup with everything except `false` should already be in place. You need to add this for all combinations of platforms(x86, x64, AnyCPU, etc.) and modes (Release,Debug, etc.) you have in there – Aleksei Poliakov Dec 02 '13 at 08:43
  • My solution contains multi projects do i need to do this to all .csproj files or only to the .csproj file in the main project? – user2717436 Dec 03 '13 at 11:14
  • Only for projects that require files to be changed in pre build event – Aleksei Poliakov Dec 03 '13 at 11:57
  • Didn't work for me unfortunately, running with an asp .net core application. I'll see if i can find a workaround. – bnns Aug 06 '20 at 06:25
  • @bnns Are you changing the .csproj file? If I change the .cs files, it works. If I change the .csproj file, it fails. – leonardzheng Aug 17 '20 at 04:29