How to implement build events in Visual C# .NET or Visual C# 2005 (810230)



The information in this article applies to:

  • Microsoft Visual C# .NET (2002)
  • Microsoft Visual C# .NET (2003)
  • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C# 2005, Express Edition

IMPORTANT: This article contains information about modifying the registry. Before you modify the registry, make sure to back it up and make sure that you understand how to restore the registry if a problem occurs. For information about how to back up, restore, and edit the registry, click the following article number to view the article in the Microsoft Knowledge Base:

256986 Description of the Microsoft Windows Registry

SUMMARY

This step-by-step article describes how to implement build events in Visual C# .NET or Visual C# 2005.

When you use build events, you can specify an action that occurs at a specific time during the build process. For example, you can specify a post-build event to register a file by using regsvr32.exe. You can also view and make a change to a generated assembly in the GAC (Global Assembly Cache) by using the Gacutil.exe tool.

In Visual C# .NET or Visual C# 2005, there are no direct methods to specify pre-build and post-build events.

back to the top

Use a Visual C++ Makefile Project to Implement Build Events

Build events are a built-in feature of Microsoft Visual C++ .NET projects. You can implement these events in a Visual C# .NET or Visual C# 2005 project by including a Visual C++ .NET Makefile project in the solution. To implement pre-build events, you must make the Visual C++ .NET Makefile project dependant on the Visual C# .NET or Visual C# 2005 project. To implement post-build events, you must make the Visual C# .NET or Visual C# 2005 project dependant on the Visual C++ .NET Makefile project.

To do this, follow these steps:
  1. Open your existing Visual C# .NET or Visual C# 2005 solution.
  2. On the File menu, point to Add Project, and then click New Project.
  3. Under Project Types, click Visual C++ Projects, and under Templates, click MakeFile Project.
    Note In Visual Studio 2005, "Visual C++ Projects" should be changed to "Visual C++."
  4. Type the name for the solution, specify a location for the solution, and then click OK.
  5. In the Makefile Application Wizard, click Finish.
  6. On the Project menu of the Makefile Project, click Properties.
  7. In the Configuration Type box, select Utility, and then click OK.
  8. On the Project menu, click Project Dependencies.. To perform the pre-build events, follow these steps:
    1. On the Dependencies tab, select the appropriate Visual C# .NET project from the Projects list, and then under Depends on, click the Visual C++ Makefile project .
    2. Click OK to apply the change. This makes sure that the Visual C# .NET or Visual C# 2005 project is built last.
    3. Right-click the Visual C++ Makefile project in Solution Explorer, and then click Properties.
    4. Click Build Events, and then specify the pre-build event command.
      For example, to copy the file before building, specify the following:
      copy c:\AddinReg.reg $(OutDir)
  9. To perform the post-build setup, follow these steps:
    1. Click the Visual C++ Makefile project in the Projects list, and then select the appropriate Visual C# .NET or Visual C# 2005 project under Depends on.
    2. Click OK to apply the change. This makes sure that the Visual C++ project is built last.
    3. Right-click the Visual C++ Makefile project in Solution Explorer, click Properties, and then click Build Events to specify the post-build event command.
back to the top

Use the PrePostBuildRules Add-In to Implement Build Events

  1. This add-in provides pre-build and post-build rules at the solution and project levels.
    To download the add-in, see the the following Microsoft Web site:

    http://download.microsoft.com/download/VisualStudioNET/Samples/7.0/NT5XP/EN-US/BuildRules.exe

    WARNING: If you use Registry Editor incorrectly, you may cause serious problems that may require you to reinstall your operating system. Microsoft cannot guarantee that you can solve problems that result from using Registry Editor incorrectly. Use Registry Editor at your own risk.
    To use this add-in, follow these steps :
    1. Load the solution file with this source, and then build the solution.
    2. On Tools menu, click Add-In Manager. If the Add-In Manager is already open, close it, and then open it again.

      Note If the new add-in appears in the Add-In Manager list of add-ins, you have successfully registered the add-in.
    3. If the add-in does not appear in the Add-In Manager list, follow the steps to register it manually:
      1. Locate the AddinReg.reg file in the BuildRules folder.
      2. Right-click AddinReg.reg, and then click Merge. Click Yes when prompted to add the entries to the registry.
      3. The new entries made to the registry can be seen in the following location:

        HKEY_CURRENT_USER\SOFTWARE\Microsoft\VisualStudio\7.0\AddIns
    4. Start a new instance of Visual Studio .NET or Visual Studio 2005.
    5. On the Tools menu, click the new add-in.
    6. A dialog box appears that allows you to select the solution or project, and to add rules to it.
    7. When you add a rule, you can specify the name echo as the program name to display raw text in the output window.
back to the top

REFERENCES

For additional information, see the following Microsoft Web site:

Automation Samples for Visual Studio .NET
http://msdn.microsoft.com/vstudio/downloads/samples/automation.asp

Modification Type:MinorLast Reviewed:1/13/2006
Keywords:kbdownload kbHOWTOmaster kbCompiler kbhowto KB810230 kbAudDeveloper