You’ve all had it happen. You just put the finishing touches on your project and you hit the Publish button. The files are automatically copied to the file server and users start get the updates right away. You double-check the publish and then you realize that you published the DEBUG version with all your program stops, disabled error checking, and wide open access to all program features!
Custom MSBuild targets to the rescue! Just add the following to your project file and it will prevent a publish when the DEBUG constant is defined. Place it at the bottom of your project file right before the </Project> tag. (For an example of editing your project file, see my post on Obfuscating a ClickOnce Publish.)
<!-- The following makes sure we don't try to publish a configuration that defines the DEBUG constant -->
<Target Name="BeforePublish">
<Error Condition="'$(DefineDebug)'=='true'"
Text="You attempted to publish a configuration that defines the DEBUG constant!" />
</Target>
By the way, the DEBUG constant is the constant that you use in your code as a compiler constant, for example #If DEBUG Then. It can be found in the ‘Compile’ tab under ‘My Project’ by clicking ‘Advanced Compile Options…’:

