8

In Visual Studio 2008, can a post-build event be used with ClickOnce publishing? If so, how?

Out of the box, it looks like I can only use pre-build events and ClickOnce publishing seems to build the project to a different location, before the post build event is launched.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Brad
  • 115
  • 2
  • 6

2 Answers2

7

Looking at the MSBuild files Visual Studio uses, the post build event is run by the Build target. If you run msbuild from the command-line and call the Publish target directly, it definitely calls Build first. If you right-click on the project in VS and click Publish, a trimmed-down target called PublishOnly gets run, on the assumption that VS has already done a build.

Your post build event should be run by Visual Studio when it automatically builds your project prior to publishing. In the Build Events tab of your project's properties, did you set the event to "run always"?

If you want to be more explicit about what happens prior to publishing, there's a BeforePublish target that Publish always looks for, whether it's run by MSBuild or Visual Studio. Edit your project file by hand, and at the bottom you'll see a couple of commented-out Target elements. Add one of your own like this:

<Target Name="BeforePublish">
    <Exec Condition="'$(PostBuildEvent)' != ''" 
          WorkingDirectory="$(OutDir)" Command="$(PostBuildEvent)" />
</Target>

That will run the same post build event you defined in your project, but you could put any MSBuild tasks inside those Target elements.

Rory MacLeod
  • 11,012
  • 7
  • 41
  • 43
  • 4
    So my build event is to copy the correct app.config (I have a dev, test and prod version) and encrypt it. The post build event does fire (thank you) but I found does not copy this app.config into the publish manifest, it appears to select the original app.config from the project. – Brad Oct 12 '10 at 16:52
  • ClickOnce will publish anything in you project with a build action of "Content", so make sure your source app.config has a build action of None. That won't stop the app.config getting renamed to MyApp.exe.config by the build. ClickOnce will pick up the exe.config because the build lists it as an output. If you replace the exe.config in your Debug/Release folder in the BeforePublish target, ClickOnce should pick up your encrypted version. – Rory MacLeod Oct 13 '10 at 17:22
  • 1
    @Ray... but problem is ClickOnce is not picking up exe.config from Debug/Release folder. It is picking it from "App Config" whatever that is. This is line from Visual Studio's output window during a ClickOnce publish session: Copying file from "App.config" to "..\..\..\bin\MEO_D_E_Release\app.publish\Application Files\EMS.OCC600.Infrastructure.Shell_1_0_4_28\EMS.OCC600.Infrastructure.Shell.exe.config". – Klaus Nji Nov 01 '12 at 17:05
0

I think you will find blog post Tricks with app.config and ClickOnce deployment useful. It talks about having different app.config files for each type of deployment.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
RobinDotNet
  • 11,723
  • 3
  • 30
  • 33