简体   繁体   English

C# 列出已安装的证书

[英]C# list installed certificates

first i'm an complete newby on c#, so im just searching on the web for posibilities What i want to get: I want a button which retrieves a list of installed certificates in the personal store.首先,我是 C# 的完全新手,所以我只是在网上搜索可能性 我想得到什么: 我想要一个按钮,用于检索个人商店中已安装证书的列表。

i tried already a little, but get messages about missing references etc. So i hope someone can give me a litte advice how to achieve this.我已经尝试了一点,但是收到有关缺少引用等的消息。所以我希望有人能给我一些如何实现这一点的建议。

what i found on the web is:我在网上找到的是:

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

yours你的

(i use visual studio...) (我使用视觉工作室...)

  1. Add a reference to System.Security.dll to use the X509Certificate2UI class.添加对System.Security.dll的引用以使用X509Certificate2UI类。

  2. you can use foreach你可以使用 foreach

if (sel.Count > 0){
  foreach(var cert in sel){
    certSelected = cert ;
  }
}
  1. you can make it even shorte when using linq你可以在使用 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