X509Store Class
Represents an X.509 store, which is a physical store where certificates are persisted and managed. This class cannot be inherited.
Assembly: System (in System.dll)
| Name | Description | |
|---|---|---|
![]() | X509Store() | Initializes a new instance of the X509Store class using the personal certificates of the current user store. |
![]() | X509Store(IntPtr) | Initializes a new instance of the X509Store class using an Intptr handle to an HCERTSTORE store. |
![]() | X509Store(StoreLocation) | Initializes a new instance of the X509Store class using the specified StoreLocation value. |
![]() | X509Store(StoreName) | Initializes a new instance of the X509Store class using the specified StoreName value. |
![]() | X509Store(StoreName, StoreLocation) | Initializes a new instance of the X509Store class using the specified StoreName and StoreLocation values. |
![]() | X509Store(String^) | Initializes a new instance of the X509Store class using the specified store name. |
![]() | X509Store(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. |
| Name | Description | |
|---|---|---|
![]() | Certificates | Returns a collection of certificates located in an X.509 certificate store. |
![]() | Location | Gets the location of the X.509 certificate store. |
![]() | Name | Gets the name of the X.509 certificate store. |
![]() | StoreHandle | Gets an IntPtr handle to an HCERTSTORE store. |
| Name | Description | |
|---|---|---|
![]() | Add(X509Certificate2^) | Adds a certificate to an X.509 certificate store. |
![]() | AddRange(X509Certificate2Collection^) | Adds a collection of certificates to an X.509 certificate store. |
![]() | Close() | Closes an X.509 certificate store. |
![]() | Dispose() | Releases the resources used by this X509Store. |
![]() | Equals(Object^) | Determines whether the specified object is equal to the current object.(Inherited from Object.) |
![]() | GetHashCode() | Serves as the default hash function. (Inherited from Object.) |
![]() | GetType() | |
![]() | Open(OpenFlags) | Opens an X.509 certificate store or creates a new store, depending on OpenFlags flag settings. |
![]() | Remove(X509Certificate2^) | Removes a certificate from an X.509 certificate store. |
![]() | RemoveRange(X509Certificate2Collection^) | Removes a range of certificates from an X.509 certificate store. |
![]() | ToString() | Returns a string that represents the current object.(Inherited from Object.) |
Use this class to work with an X.509 store.
Important |
|---|
Starting with the .NET Framework 4.6, this type implements the IDisposable interface. When you have finished using the type, you should dispose of it either directly or indirectly. To dispose of the type directly, call its Dispose method in a try/catch block. To dispose of it indirectly, use a language construct such as using (in C#) or Using (in Visual Basic). For more information, see the “Using an Object that Implements IDisposable” section in the IDisposable interface topic. For apps that target the .NET Framework 4.5.2 and earlier versions, the X509Store class does not implement the IDisposable interface and therefore does not have a Dispose method. |
This section contains two examples. The first example demonstrates how you can open standard X.509 stores and list the number of certificates in each.
The second example demonstrates how you can add and remove single certificates and ranges of certificates.
Example 1
This example tries to open each standard store in each standard location on the current computer. It prints a summary that shows whether each store exists and, if so, the number of certificates it contains.
The example creates an X509Store object for each combination of standard name and standard location. It calls the Open method with the OpenFlags::OpenExistingOnly flag, which opens the physical store only if it already exists. If the physical store exists, the example uses the Name, Location, and Certificates properties to display the number of certificates in the store.
Example 2
This 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.
#using <System.dll> #using <System.Security.dll> using namespace System; using namespace System::Security::Cryptography; using namespace System::Security::Cryptography::X509Certificates; using namespace System::IO; int main() { //Create new X509 store called teststore from the local certificate store. X509Store ^ store = gcnew X509Store( "teststore",StoreLocation::CurrentUser ); store->Open( OpenFlags::ReadWrite ); X509Certificate2 ^ certificate = gcnew X509Certificate2; //Create certificates from certificate files. //You must put in a valid path to three certificates in the following constructors. X509Certificate2 ^ certificate1 = gcnew X509Certificate2( "c:\\mycerts\\*****.cer" ); X509Certificate2 ^ certificate2 = gcnew X509Certificate2( "c:\\mycerts\\*****.cer" ); X509Certificate2 ^ certificate5 = gcnew X509Certificate2( "c:\\mycerts\\*****.cer" ); //Create a collection and add two of the certificates. X509Certificate2Collection ^ collection = gcnew X509Certificate2Collection; collection->Add( certificate2 ); collection->Add( certificate5 ); //Add certificates to the store. store->Add( certificate1 ); store->AddRange( collection ); X509Certificate2Collection ^ storecollection = dynamic_cast<X509Certificate2Collection^>(store->Certificates); Console::WriteLine( "Store name: {0}", store->Name ); Console::WriteLine( "Store location: {0}", store->Location ); System::Collections::IEnumerator^ myEnum = storecollection->GetEnumerator(); while ( myEnum->MoveNext() ) { X509Certificate2 ^ x509 = safe_cast<X509Certificate2 ^>(myEnum->Current); Console::WriteLine( "certificate name: {0}", x509->Subject ); } //Remove a certificate. store->Remove( certificate1 ); X509Certificate2Collection ^ storecollection2 = dynamic_cast<X509Certificate2Collection^>(store->Certificates); Console::WriteLine( "{1}Store name: {0}", store->Name, Environment::NewLine ); System::Collections::IEnumerator^ myEnum1 = storecollection2->GetEnumerator(); while ( myEnum1->MoveNext() ) { X509Certificate2 ^ x509 = safe_cast<X509Certificate2 ^>(myEnum1->Current); Console::WriteLine( "certificate name: {0}", x509->Subject ); } //Remove a range of certificates. store->RemoveRange( collection ); X509Certificate2Collection ^ storecollection3 = dynamic_cast<X509Certificate2Collection^>(store->Certificates); Console::WriteLine( "{1}Store name: {0}", store->Name, Environment::NewLine ); if ( storecollection3->Count == 0 ) { Console::WriteLine( "Store contains no certificates." ); } else { System::Collections::IEnumerator^ myEnum2 = storecollection3->GetEnumerator(); while ( myEnum2->MoveNext() ) { X509Certificate2 ^ x509 = safe_cast<X509Certificate2 ^>(myEnum2->Current); Console::WriteLine( "certificate name: {0}", x509->Subject ); } } //Close the store. store->Close(); }
Available since 2.0
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.


