Visual Studio Project File

Summary

This blog post is written for developers that are new to Visual Studio, or have not ventured outside the programming environment itself.  I’m going to show a few handy features that are available in the project file itself.

The Project File

Any time you create a new project a project file gets created.  Your first project will be inside of the solution folder and be contained inside the solution.  Each project you add to a solution will create a new project folder and project file.  The project file extension for a C# project is “csproj”.  This file is an xml file which you can edit with any text editor.

The solution file can also be edited and this can be handy if you have to fix a problem.  One problem that occurs quite often is if you rename a project folder from windows.  You can just edit the .sln file and change the project directory name.


BeforeBuild and AfterBuild

Back to the project file…

At the bottom of the project file is a commented section called BeforeBuild and AfterBuild.  The purpose of this section is to add commands to perform tasks before a build starts or after a build ends.  I have used this section to copy the newly built dlls into the destination directory.  First, you’ll need to remove the comments:

<!– To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets.
<Target Name=”BeforeBuild”>
</Target>
<Target Name=”AfterBuild”>
</Target>
–>



Next, you can add a copy command inside the AfterBuild xml tags:

<Target Name=”BeforeBuild”>
</Target>
<Target Name=”AfterBuild”>

<Copy SourceFiles=”$(TargetDir)$(ProjectName).exe” 
  DestinationFolder=”.” ContinueOnError=”true” />
</Target>

The current directory specified will be the project directory.  By using the $(TargetDir) variable, you can grab the dll or exe file from the directory that it was just built to.  Otherwise you’ll need to know if it went into the BinDebug or BinRelease folder.  Using the $(ProjectName) variable guarentees that the filename is correct, since this will be the name of the exe.  However, if you want to copy other dlls or files, you’ll need specify them directly.

What is a good use for the BeforeBuild?

Now you might be contemplating why you would want to put anything in the BeforeBuild section.  You might have a project where you would generate some C# source code depending on data in a database.  An instance is similar to an ORM generator such as the NHibernate code generator that I developed (see some of my previous blog posts).  In such a case, you might want the convenience of regenerating your ORM definition just before building a particular application.

You could also have a project that uses a language generator such as YACC or ANTLR.  The language generator would be called before build, then the resulting C# source could be copied into your project and then the build could proceed to build your program.

Troubleshooting BeforeBuild and AfterBuild

 Create a new console project and edit the project file.  Change the AfterBuild to this:

<Target Name=”AfterBuild”>
<Copy SourceFiles=”no.exe” DestinationFolder=”.” ContinueOnError=”true” />
</Target>

Don’t forget to uncomment this section.  Now open your project and select Build -> Rebuild Solution.  You should see a warning:

As you can see, there is an error that the file was not found, then it tells the file that the error was in, the line number and column.  If you remove the “ContinueOnError” section, you’ll get an error message instead of a warning.  Your build will stop at that point, though the normal build has completed because this occurs after your build.  If you get an error in the BeforeBuild section, your build will not occur.

List of all Macros Used by BeforeBuild and AfterBuild

You can go to Micorosofts site to get a list of all the macros that are available:

Macros for Build Commands and Properties 

 

Leave a Reply