检测是否存在指定的证书:
/// <summary> /// 检测是否存在指定的证书 /// </summary> /// <returns></returns> static bool CheckCert() { bool result = false; X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); try { store.Open(OpenFlags.ReadWrite); foreach (var item in store.Certificates) { if (item.SubjectName.Name == "CN=Client.IOGS.PeopleDaily.BQH") { result = true; } } } finally { store.Close(); } return result; }
安装证书:
需要以管理员权限运行
string certPath = Path.Combine(Application.StartupPath, "Client.IOGS.PeopleDaily.BQH.pfx"); X509Certificate2 certificate = new X509Certificate2(certPath, "password01!"); X509Store store = new X509Store(StoreName.My, StoreLocation.LocalMachine); store.Open(OpenFlags.ReadWrite); store.Remove(certificate); //可省略 store.Add(certificate); store.Close();