C#程序自动安装数字证书

时间:2024-02-21 07:03:26

using System.Security.Cryptography.X509Certificates;

 

MessageBox.Show("开始");

 

//添加个人证书

X509Certificate2 certificate = new X509Certificate2(Application.StartupPath + \\cert.pfx","证书密码");
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
store.Remove(certificate);   //可省略
store.Add(certificate);
store.Close();

 

//安装CA的根证书到受信任根证书颁发机构
certificate = new X509Certificate2(Application.StartupPath + "\\CA.cer");
store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadWrite);
store.Remove(certificate);   //可省略
store.Add(certificate);
store.Close();

           
MessageBox.Show("结束");