Link to home
Start Free TrialLog in
Avatar of rwheeler23
rwheeler23Flag for United States of America

asked on

Getting the PublicKeyToken using C#

I need to find the PublicKeyToken of an assembly called Application.Dynamics

Here is my method for getting the public key token.
====================================================================================================================
        private static string GetPublicKeyTokenFromAssembly(Assembly assembly)
        {
            var bytes = assembly.GetName().GetPublicKeyToken();
            if (bytes == null || bytes.Length == 0)
                return "None";

            var publicKeyToken = string.Empty;
            for (int i = 0; i < bytes.GetLength(0); i++)
                publicKeyToken += string.Format("{0:x2}", bytes[i]);
            return publicKeyToken;
        }

===================================================================================
The question is how to I assign this value to a string?
string TokenString = GetPublicKeyTokenFromAssembly("Application.Dynamics");

This does not work. I get a message about not being able to convert a string to an assembly reference.
How do structure so I can see the value of the Public Key Token of this assembly?
Avatar of Alfredo Luis Torres Serrano
Alfredo Luis Torres Serrano
Flag of United States of America image

Use the following command,

sn –T myDLL.dll

This will give you the public key token. Remember one thing this only works if the assembly has to be strongly signed.

Example
C:\WINNT\Microsoft.NET\Framework\v3.5>sn -T EdmGen.exe

Microsoft (R) .NET Framework Strong Name Utility  Version 3.5.21022.8

Copyright (c) Microsoft Corporation.  All rights reserved.

Public key token is b77a5c561934e089
ASKER CERTIFIED SOLUTION
Avatar of it_saige
it_saige
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rwheeler23

ASKER

Thanks