要汇总代码, 枚举 " My " 证书存储中证书, 填充 ListView 控件使用的每个证书, 主题和颁发者名称并然后附加到 ListView 项的 标记 属性证书上下文指针。
private void button1_Click(object sender, System.EventArgs e)
{
listView1.Items.Clear();
uint hCertStore = Crypt32.CertOpenSystemStore(0, "My");
if (hCertStore == 0)
{
MessageBox.Show("CertOpenSystemStore failed: " + Marshal.GetLastWin32Error().ToString());
return;
}
uint pCertContext = Crypt32.CertEnumCertificatesInStore(hCertStore, (uint)0);
while (pCertContext != 0)
{
X509Certificate x509 = new X509Certificate((IntPtr)pCertContext);
ListViewItem NewItem = listView1.Items.Add(x509.GetName());
NewItem.SubItems.Add(x509.GetIssuerName());
// Increment the reference count so that you can use this value later.
NewItem.Tag = Crypt32.CertDuplicateCertificateContext(pCertContext);
pCertContext = Crypt32.CertEnumCertificatesInStore(hCertStore, pCertContext);
}
}