簡體   English   中英

C# 列出已安裝的證書

[英]C# list installed certificates

首先,我是 C# 的完全新手,所以我只是在網上搜索可能性 我想得到什么: 我想要一個按鈕,用於檢索個人商店中已安裝證書的列表。

我已經嘗試了一點,但是收到有關缺少引用等的消息。所以我希望有人能給我一些如何實現這一點的建議。

我在網上找到的是:

using System.Security.Cryptography.X509Certificates;
        public static X509Certificate2 selectCert(StoreName store, StoreLocation location, string windowTitle, string windowMsg)
    {

        X509Certificate2 certSelected = null;
        X509Store x509Store = new X509Store(store, location);
        x509Store.Open(OpenFlags.ReadOnly);

        X509Certificate2Collection col = x509Store.Certificates;
        X509Certificate2Collection sel = X509Certificate2UI.SelectFromCollection(col, windowTitle, windowMsg, X509SelectionFlag.SingleSelection);

        if (sel.Count > 0)
        {
            X509Certificate2Enumerator en = sel.GetEnumerator();
            en.MoveNext();
            certSelected = en.Current;
        }

        x509Store.Close();

        return certSelected;
    }

你的

(我使用視覺工作室...)

  1. 添加對System.Security.dll的引用以使用X509Certificate2UI類。

  2. 你可以使用 foreach

if (sel.Count > 0){
  foreach(var cert in sel){
    certSelected = cert ;
  }
}
  1. 你可以在使用 linq 時讓它更短
using System.Linq;
...

if (sel.Count > 0){
  return sel.FirstOrDefault();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM