X509Store.Open Method (OpenFlags)


Opens an X.509 certificate store or creates a new store, depending on OpenFlags flag settings.

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

Public Sub Open (
	flags As OpenFlags


Type: System.Security.Cryptography.X509Certificates.OpenFlags

A bitwise combination of enumeration values that specifies the way to open the X.509 certificate store.

Exception Condition

The store is unreadable.


The caller does not have the required permission.


The store contains invalid values.

Use this method to open an existing X.509 store. Note that you must have additional permissions, specified by StorePermissionFlags, to enumerate the certificates in the store. You can create a new store by passing a store name that does not exist to the class constructor, and then using any of the OpenFlags flags except OpenExistingOnly.

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)
        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()

        'Add certificates to the store.

        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.
        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.
        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.")
            Dim x509b As X509Certificate2
            For Each x509b In storecollection3
                Console.WriteLine("certificate name: {0}", x509b.Subject)
            Next x509b
        End If

        'Close the store.

    End Sub
End Class


for opening an X.509 certificate store. Security action: Demand. Associated enumerations: StorePermissionFlags.OpenStore, StorePermissionFlags.CreateStore

.NET Framework
Available since 2.0
Return to top