Monday, February 20, 2006

GacIT

Disclaimer: My geeky side (nor my cool side for that metters) can not be held responsible for any damage that might be caused to your computer or any loss of data that may or may not result after you follow the procedures described in this tech note. This tech note contains steps that might ask you manipulate your machine's settings and/or registry keys, if you choose to follow these instructions then you do so at your own risk.


The other day, we were placing some assemblies in the GAC (Global Assembly Cache) and we just thought: "Isn't there and easier way to place it in the GAC? Drag and Drop is just too much work!" Lazy, can't argue, but that's us ;-)

So I thought: "Why won't we do some thing that will allow us to send an assembly to the GAC by right-clicking it!". Here's how I did it.

Option 1 (Right-click -> Send To - > GacUtil.exe):
1. Go to Start -> Run
2. Type "sendto", this will open the SendTo folder containing the shortcuts that appear on the send to menu when you right click an file
3. Right click the folder and select New -> Shortcut
4. For the location option, enter the path to the gacutil.exe, and add the /i switch at the end
5. Click Next and enter GAC as the name of the shortcu, then finish the wizard

Option 1 works fine if you have the default installation on your machine. But you don't always get gacutil in the same folder? Of course, you can always go and look it up, but there is always another "but" on the way. I would like to have a small script that I can run on every machine that I use to set up a GacIT feature for assemblies. This is why I came up with option 2.

Option 2:
1. For this option, I need to have a small c# app that is only around a dozen lines of code. This app will be responsible for programmatically installing an assembly into the GAC. So open up you Visual Studio and create a new C# console application (call it GacIT) and enter the following code in its main function

static void Main(string[] args)
{
try
{
if (args.Length > 0)
{
System.EnterpriseServices.Internal.Publish gacPublish =
new System.EnterpriseServices.Internal.Publish();

string assemblyPath = args[0];

System.Reflection.Assembly assemblyToGac = System.Reflection.Assembly.LoadFrom(assemblyPath);

if (assemblyToGac.GetName().GetPublicKey().Length > 0)
{
gacPublish.GacInstall(assemblyPath);
}
else
{
throw new Exception("Assembly not signed");
}

System.Windows.Forms.MessageBox.Show("Assembly successfully added to the GAC", "GacIT");
}

}
catch (System.Security.SecurityException ex)
{
System.Windows.Forms.MessageBox.Show("Assembly cannot be added to the GAC. Insufficent Security Permissions." + ex.PermissionType.ToString(), "GacIT");
}
catch (Exception ex)
{
System.Windows.Forms.MessageBox.Show("Assembly cannot be added to the GAC. " + ex.Message, "GacIT");
}
}

P.S. This is .NET 2.0 code and is not supported in previous versions
2. Build the project and get the exe output
3. What we will do now, is to add a new shortcut to the shell menu of the .dll files on the machine. This shortcut will be available when right-clicking all .dll files. To do that, open my favorite app ever, notepad
4. Enter the following registry key information in it

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\dllfile\Shell\GacIT]
@="GacIT"

[HKEY_CLASSES_ROOT\dllfile\Shell\GacIT\Command]
@="E:\\GacIT\\GacIT.exe \"%1\""

P.S. Remember to change the "E:\\GacIT" path to the corresponding path on your machine

5. Save the notepad file as GacIT.reg
6. Now you are ready to add GacIT to your system and any other system that you might use

Now, you can play with this and add whatever feature you might like (i.e. Un-GacIT), or if you like, and you work with COM+ as well, then the Publish class provides the ability to register COM+ applications.

If you need any information on the classes used in the code above, or the Windows Shell and extending it, please drop me a comment below.

sk8er boi

0 Comments:

Post a Comment

<< Home