Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import an .snk file generated with sn.exe in .NET?

I have created a key file with this command:


 sn.exe -k 2048 Build.snk

I would like to read in this key with .NET. I haven't been able to find any document as to the format of the .snk, so I'm wondering if there's a way in C# to read in an .snk file?

My original reason for asking the question was to use the .snk file for purposes other than signing an assembly. As one of the answers states below, the purpose of .snk files is really just for signing assemblies.

like image 536
Steve Wranovsky Avatar asked Dec 12 '22 21:12

Steve Wranovsky


1 Answers

Found a mention in MSDN here. Here's the code:


public static void Main()
{
    // Open a file that contains a public key value. The line below  
    // assumes that the Strong Name tool (SN.exe) was executed from 
    // a command prompt as follows:
    //       SN.exe -k C:\Company.keys
    using (FileStream fs = File.Open("C:\\Company.keys", FileMode.Open))
    {
        // Construct a StrongNameKeyPair object. This object should obtain
        // the public key from the Company.keys file.
        StrongNameKeyPair k = new StrongNameKeyPair(fs);

        // Display the bytes that make up the public key.
        Console.WriteLine(BitConverter.ToString(k.PublicKey));

        // Close the file.
        fs.Close();
    }
}

like image 132
Steve Wranovsky Avatar answered Jan 08 '23 04:01

Steve Wranovsky