How to add an Assembly automatically into GAC using Post Build Events in Visual Studio

Introduction: When we build a project, then the dll generated will not be added to GAC.
If we need to dll into the GAC we need to explicitly added it using the Visual Studio command prompt
There are scenarios where we need to add an assembly into the GAC after building the Visual Studio Project. To do this we can use the Post Build Events in VS.

Scripts inside Post Build Events will be executed once the build of the project is successful.

Steps to use Post Build Events in Visual Studio
Step 1) Right click the project Select Properties

Step 2) Select Build Events in the Left Pane
image

Step 3) Click on Edit Post-Build button, In the window that opens enter the below command (with the quotes)-
"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\GacUtil.exe" /if "$(TargetPath)"
Remember to change the path for the GacUtil based on the version of DotNet

TargetPath is a macro contains the path for the current project dll and can be seen below screenshot.
(Available Macros can be seen by clicking on the Macros button)

If we have any custom dll to be installed then we can give the path with the dll name instead of the macro TargetPath

image

Gacutil path in different versions
Visual Studio 2005 - C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\
Visual Studio 2008 - C:\Program Files\Microsoft SDKs\Windows\v6.0A\bin\
Visual Studio 2010 - C:\Program Files\Microsoft SDKs\Windows\v7.0A\bin\
Visual Studio 2010 (64 bit) - C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin
 
Uses: 

Links: 

Pre-build Event/Post-build Event Command Line Dialog Box

https://msdn.microsoft.com/en-us/library/42x5kfw4.aspx









No comments: