System.Net.Security.CertificateStoreLocation Enum

Specifies the location of certificate stores.

Members

Member name Description
CurrentUser Certificates are stored in the certificate store of the current user.
LocalMachine Certificates are stored in the certificate store of the local computer.

Remarks

The CertificateStoreLocation enumeration is used to specify which certificate store to open. For example, you can use CertificateStoreLocation.CurrentUser to open the certificate store for the current user or CertificateStoreLocation.LocalMachine to open the certificate store for the local computer.

This enumeration is used by the following methods:

Example

Using CertificateStoreLocation.CurrentUser

The following code example demonstrates how to open the personal certificate store for the current user.

using System;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

public class Example
{
    public static void Main(string[] args)
    {
        try
        {
            // Open the personal certificate store for the current user.
            X509Store store = new X509Store(StoreName.My, CertificateStoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly);

            Console.WriteLine("Successfully opened the personal certificate store for the current user.");

            // You can now access certificates within the store.
            if (store.Certificates.Count > 0)
            {
                Console.WriteLine($"Number of certificates found: {store.Certificates.Count}");
            }
            else
            {
                Console.WriteLine("No certificates found in the personal store.");
            }

            store.Close();
        }
        catch (CryptographicException e)
        {
            Console.WriteLine($"A CryptographicException was caught: {e.Message}");
        }
        catch (Exception e)
        {
            Console.WriteLine($"An unexpected exception was caught: {e.Message}");
        }
    }
}

Using CertificateStoreLocation.LocalMachine

The following code example demonstrates how to open the root certificate store for the local machine.

using System;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

public class Example
{
    public static void Main(string[] args)
    {
        try
        {
            // Open the root certificate store for the local computer.
            X509Store store = new X509Store(StoreName.Root, CertificateStoreLocation.LocalMachine);
            store.Open(OpenFlags.ReadOnly);

            Console.WriteLine("Successfully opened the root certificate store for the local machine.");

            // You can now access certificates within the store.
            if (store.Certificates.Count > 0)
            {
                Console.WriteLine($"Number of certificates found: {store.Certificates.Count}");
            }
            else
            {
                Console.WriteLine("No certificates found in the root store.");
            }

            store.Close();
        }
        catch (CryptographicException e)
        {
            Console.WriteLine($"A CryptographicException was caught: {e.Message}");
        }
        catch (Exception e)
        {
            Console.WriteLine($"An unexpected exception was caught: {e.Message}");
        }
    }
}

Requirements

Attribute Value
Namespace System.Net.Security
Assembly System.Net.Primitives.dll
.NET Framework Available in: .NET Framework 4.5, .NET Framework 4.6, .NET Framework 4.7, .NET Framework 4.8
.NET Core Available in: .NET Core 1.0, .NET Core 1.1, .NET Core 2.0, .NET Core 2.1, .NET Core 2.2, .NET Standard 1.3, .NET Standard 2.0