2010-06-22 169 views
3

我創建了一個密鑰文件使用此命令:如何導入使用.NET中的sn.exe生成的.snk文件?

 

sn.exe -k 2048 Build.snk 
 

我想在這個關鍵與.NET閱讀。我一直無法找到關於.snk格式的任何文檔,所以我想知道是否有一種方法在C#中讀取.snk文件?

我問這個問題的原因是爲了簽署程序集之外的其他目的使用.snk文件。作爲下面的答案之一,.snk文件的目的實際上只是用於簽署程序集。

回答

9

發現在MSDN here一提。代碼如下:

 

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(); 
    } 
}