Tuesday, September 16, 2008

Get Assembly Strong Name in Context Menu


Hi guys!

This will be my first topic in my “blogging career” and I hope it will be useful.

What is the main idea?

Maybe every .NET developer at times it is required to know the version of some assembly, or whether it’s signed or not. To get this you have to see the Strong Name of assembly, which it’s consisted from Name, Version Number, Culture and Public Key Token. And this will have to do it quite often. The way to do this is to open assembly in Reflector, Visual Studio or Dependency Killer (this is a great tool of my friend Vesko Kolev). My main idea is to create faster way to do this - application, which gives you option to get strong name of assembly in context menu of Windows.

To add something in context menu of Windows we have to get involved in windows registry. But what is this? Registry is “hierarchical database used in Microsoft to store information that is necessary to configure the system for one or more users, applications and hardware devices”. “Each branch is called a Key. Each key can contain other keys, as well as Values. Each value contains the actual information stored in the Registry.” All registries are stored in two files in your Windows directory, USER.DAT and SYSTEM.DAT. Don’t touch them!

Before you make any change of registry you have to create backup. Look here.

I created helping tool that added all you need in registry to get your command in context menu, which should start application that I previously mentioned.

You can download source code from here.

Only thing you have to do is to choose name of command and select full path to the executable file of the application (GetAssemblyStrongName.exe).

But what Registry Editor does in the background? I will show how you can do everything by yourself that Registry Editor does and that I will explain it.

1. Type regedit in Run window to open Registry Editor of Windows.

2. Go to “HKEY_LOCAL_MACHINE\SOFTWARE\Classes\dllfiles”

3. If there is no key with name “shell” you have to create it.

4. In “shell” key add other one with name, which you want to show in context menu. If placed not in “shell, the command name will not appear in context menu.

5. In the key, created in the previous step, create one more with the name “command”

6. In the key, created in the previous step, set the (Default) value data of “PATH_TO_THE_PROGRAM %1”, where PATH_TO_THE_PROGRAM is full path to GetAssemblyStrongName.exe.

This “%1” in the end of value data is placeholder for current file on which you click to show context menu. Actually I pass the full path of file as a command line argument on the application.

If you look at other keys in “HKEY_LOCAL_MACHINE\SOFTWARE\Classes” which corresponds to other extensions like “txtfile”, “mp3file”, etc. you will see the same keys structure that I described.

The application uses RegistryKey class in Microsoft.Win32 namespace to open and create registry key in the system. This class is disposable object and should be closed after you finish work with it. To set (Default) value data of key I am using SetValue method of RegistryKey class with first parameter empty string.

Last thing that I should explain is the Get Assembly Strong Name application. It is very simple and the only thing it does is to get command line argument and to show in textbox the information for assembly. For presentation I use Windows Forms. Here is the code.


Conclusion

In this topic I don’t show anything hard and revolutionary, but I want show you how can create something useful with not much efforts. And in the meantime you learn something.