Windows apps
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

X509Store Class

Represents an X.509 store, which is a physical store where certificates are persisted and managed. This class cannot be inherited.

System.Object
  System.Security.Cryptography.X509Certificates.X509Store

Namespace:  System.Security.Cryptography.X509Certificates
Assembly:  System (in System.dll)

'Declaration
Public NotInheritable Class X509Store

The X509Store type exposes the following members.

  NameDescription
Public methodX509StoreInitializes a new instance of the X509Store class using the personal certificates of the current user store.
Public methodX509Store(IntPtr)Initializes a new instance of the X509Store class using an Intptr handle to an HCERTSTORE store.
Public methodX509Store(StoreLocation)Initializes a new instance of the X509Store class using the specified StoreLocation value.
Public methodX509Store(StoreName)Initializes a new instance of the X509Store class using the specified StoreName value.
Public methodX509Store(String)Initializes a new instance of the X509Store class using the specified store name.
Public methodX509Store(StoreName, StoreLocation)Initializes a new instance of the X509Store class using the specified StoreName and StoreLocation values.
Public methodX509Store(String, StoreLocation)Initializes a new instance of the X509Store class using a string that represents a value from the StoreName enumeration and a value from the StoreLocation enumeration.
Top

  NameDescription
Public propertyCertificatesReturns a collection of certificates located in an X.509 certificate store.
Public propertyLocationGets the location of the X.509 certificate store.
Public propertyNameGets the name of the X.509 certificate store.
Public propertyStoreHandleGets an IntPtr handle to an HCERTSTORE store.
Top

  NameDescription
Public methodAddAdds a certificate to an X.509 certificate store.
Public methodAddRangeAdds a collection of certificates to an X.509 certificate store.
Public methodCloseCloses an X.509 certificate store.
Public methodEquals(Object)Determines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodOpenOpens an X.509 certificate store or creates a new store, depending on OpenFlags flag settings.
Public methodRemoveRemoves a certificate from an X.509 certificate store.
Public methodRemoveRangeRemoves a range of certificates from an X.509 certificate store.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

Use this class to work with an X.509 store.

The following code example opens an X.509 certificate store, adds and deletes certificates, and then closes the store. It assumes that you have three certificates to add to and remove from a local store.


Imports System
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Imports System.IO



Class X509store2

    Shared Sub Main(ByVal args() As String)
        'Create new X509 store called teststore from the local certificate store.
        Dim store As New X509Store("teststore", StoreLocation.CurrentUser)
        store.Open(OpenFlags.ReadWrite)
        Dim certificate As New X509Certificate2()

        'Create certificates from certificate files.
        'You must put in a valid path to three certificates in the following constructors.
        Dim certificate1 As New X509Certificate2("c:\mycerts\*****.cer")
        Dim certificate2 As New X509Certificate2("c:\mycerts\*****.cer")
        Dim certificate5 As New X509Certificate2("c:\mycerts\*****.cer")

        'Create a collection and add two of the certificates.
        Dim collection As New X509Certificate2Collection()
        collection.Add(certificate2)
        collection.Add(certificate5)

        'Add certificates to the store.
        store.Add(certificate1)
        store.AddRange(collection)

        Dim storecollection As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)
        Console.WriteLine("Store name: {0}", store.Name)
        Console.WriteLine("Store location: {0}", store.Location)
        Dim x509 As X509Certificate2
        For Each x509 In storecollection
            Console.WriteLine("certificate name: {0}", x509.Subject)
        Next x509

        'Remove a certificate.
        store.Remove(certificate1)
        Dim storecollection2 As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)
        Console.WriteLine("{1}Store name: {0}", store.Name, Environment.NewLine)
        Dim x509a As X509Certificate2
        For Each x509a In storecollection2
            Console.WriteLine("certificate name: {0}", x509a.Subject)
        Next x509a

        'Remove a range of certificates.
        store.RemoveRange(collection)
        Dim storecollection3 As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)
        Console.WriteLine("{1}Store name: {0}", store.Name, Environment.NewLine)
        If storecollection3.Count = 0 Then
            Console.WriteLine("Store contains no certificates.")
        Else
            Dim x509b As X509Certificate2
            For Each x509b In storecollection3
                Console.WriteLine("certificate name: {0}", x509b.Subject)
            Next x509b
        End If

        'Close the store.
        store.Close()

    End Sub
End Class


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions

Show:
© 2017 Microsoft