Microsoft Learn

CertificateStoreCollection Class

Represents a collection of certificate stores.

Overview

The CertificateStoreCollection class provides a way to access and manage multiple certificate stores on a system. It implements the generic ICollection<CertificateStore> interface, allowing you to iterate through, add, remove, and check for the existence of CertificateStore objects.

Syntax

public sealed class CertificateStoreCollection : ICollection<CertificateStore>

Constructors

Name Description
CertificateStoreCollection() Initializes a new instance of the CertificateStoreCollection class.

Properties

Name Description
Count Gets the number of certificate stores in the collection.
IsReadOnly Gets a value indicating whether the collection is read-only.

Methods

Name Description
Add(CertificateStore store) Adds a certificate store to the collection.
Clear() Removes all certificate stores from the collection.
Contains(CertificateStore store) Determines whether a specific certificate store is in the collection.
CopyTo(CertificateStore[] array, int arrayIndex) Copies the certificate stores from the collection to an array, starting at the specified array index.
GetEnumerator() Returns an enumerator that iterates through the collection.
Remove(CertificateStore store) Removes the first occurrence of a specific certificate store from the collection.

Remarks

The CertificateStoreCollection is typically used to manage certificates stored on the local machine or in specific user profiles. You can use it to enumerate available stores, access individual stores, and perform operations on the certificates within those stores.

Examples

The following C# code example demonstrates how to retrieve all certificate stores and iterate through them.

using System;
using System.Net.Security;
using System.Collections.Generic;

public class CertificateStoreExample
{
    public static void Main(string[] args)
    {
        // Get all certificate stores
        X509StoreCollection stores = new X509StoreCollection();

        foreach (X509Store store in stores)
        {
            Console.WriteLine($"Store Name: {store.Name}");
            Console.WriteLine($"Store Location: {store.StoreLocation}");
            Console.WriteLine("---");
        }
    }
}

Note: The example above uses X509StoreCollection and X509Store which are concrete implementations that would typically be used in practice when dealing with X.509 certificates. The CertificateStoreCollection class serves as a more general abstraction.