Introduction
I develop in Visual Studio, and I'm switching from XP on my workstation to Window 7 x64. One thing that annoyed me was that I can’t simply drag .NET library DLLs to the assembly folder to install them in the GAC, or select and delete them from the assembly folder to remove them from the GAC, as I had done on Windows XP. When I try to, I get an ‘access denied’ error, even though my domain login is in the Administrators group, and even though I start Windows Explorer with ‘Run as administrator’.
Apparently, Microsoft really wants you to create installation applets to install or remove libraries in the GAC. An alternative is to log on as the actual machine administrator, in which case, drag and drop to the GAC will work.
I like being able to easily add or delete libraries to the GAC on a development workstation, however, so I came up with a work-around to let me do this.
Background
The method is to create .bat command files that use gacutil.exe to add or delete the library, to capture the name of the library when the library file is dragged to the command file, and have the command file run as administrator. Windows 7 doesn’t allow you to set a .bat command file to run as administrator, so a shortcut is created for each command file, and the shortcut is set to run as administrator.
Using the Code
This method requires gacutil.exe. If you've installed Visual Studio, it should be somewhere on your PC. After installing Visual Studio 2008 on Windows 7 x64, I found it at: C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin. I've also found it at C:\Windows\Microsoft.NET\Framework\v1.1.4322 on a machine with XP and Visual Studio 2005. I copy gacutil to the same folder where I put the command files, for convenience (I use c:\bin).
Here's the code for the AddToGAC.bat command file that installs the library:
c:\bin\gacutil.exe /i %1
pause
Here's the code for the DeleteFromGAC.bat command file that deletes the library:
c:\bin\gacutil.exe /u %~n1
pause
Note that the pause statement is optional. AddToGAC.bat uses the complete path\name of the library file that was dragged and dropped, found in the %1 variable (the path\name becomes the first command parameter). DeleteFromGAC uses %~n1 to parse the file name (without the file extension) from the path\name, which is what gacutil wants to delete a library.
I found info on parsing the path\name command parameter here: http://windowsitpro.com/article/articleid/71819/jsi-tip-0494---how-to-parse-a-batch-parameter.html.
The last step is to create a shortcut to each of the command files on the desktop. Open the properties of each shortcut, click 'Advanced', and select 'Run as administrator'.
When you drag and drop a .NET strong-named library to the Add To GAC shortcut, the User Account Control dialog box will appear (it may me minimized). Click 'Yes', and the library will be added to the GAC. Likewise, when you drag and drop a library to the Delete From GAC shortcut, the same thing will happen, and the library will be removed from the GAC. Unfortunately, you can't drag the library from the assembly folder to delete it, you have to drag it from somewhere else - Windows won't allow dragging and dropping from the GAC.
Points of Interest
I'm a newbie on Window 7, so there may be a better way to do this. One thing I'll look into is if a policy setting somewhere could grant my domain user login rights to do this directly, as though I had logged on as the machine's administrator.
Please feedback if anyone has a better way.