X500DistinguishedName.Format(Boolean) Method

Definition

Returns a formatted version of an X500 distinguished name for printing or for output to a text window or to a console.

public:
 override System::String ^ Format(bool multiLine);
public override string Format (bool multiLine);
override this.Format : bool -> string
Public Overrides Function Format (multiLine As Boolean) As String

Parameters

multiLine
Boolean

true if the return string should contain carriage returns; otherwise, false.

Returns

A formatted string that represents the X500 distinguished name.

Examples

The following code example shows how to use the X500DistinguishedName class.

#using <System.dll>
#using <system.security.dll>

using namespace System;
using namespace System::Security::Cryptography;
using namespace System::Security::Permissions;
using namespace System::IO;
using namespace System::Security::Cryptography::X509Certificates;
int main()
{
   try
   {
      X509Store ^ store = gcnew X509Store( "MY",StoreLocation::CurrentUser );
      store->Open( static_cast<OpenFlags>(OpenFlags::ReadOnly | OpenFlags::OpenExistingOnly) );
      X509Certificate2Collection ^ collection = dynamic_cast<X509Certificate2Collection^>(store->Certificates);
      X509Certificate2Collection ^ fcollection = dynamic_cast<X509Certificate2Collection^>(collection->Find( X509FindType::FindByTimeValid, DateTime::Now, false ));
      X509Certificate2Collection ^ scollection = X509Certificate2UI::SelectFromCollection(fcollection, "Test Certificate Select","Select a certificate from the following list to get information on that certificate",X509SelectionFlag::MultiSelection);
      Console::WriteLine( "Number of certificates: {0}{1}", scollection->Count, Environment::NewLine );
      System::Collections::IEnumerator^ myEnum = scollection->GetEnumerator();
      while ( myEnum->MoveNext() )
      {
         X509Certificate2 ^ x509 = safe_cast<X509Certificate2 ^>(myEnum->Current);
         X500DistinguishedName ^ dname = gcnew X500DistinguishedName( x509->SubjectName );
         Console::WriteLine( "X500DistinguishedName: {0}{1}", dname->Name, Environment::NewLine );
         x509->Reset();
      }
      store->Close();
   }
   catch ( CryptographicException^ ) 
   {
      Console::WriteLine( "Information could not be written out for this certificate." );
   }

}
using System;
using System.Security.Cryptography;
using System.Security.Permissions;
using System.IO;
using System.Security.Cryptography.X509Certificates;

class X500Sample
{
    static void Main()
    {
        try
        {
            X509Store store = new X509Store("MY", StoreLocation.CurrentUser);
            store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
            X509Certificate2Collection collection = (X509Certificate2Collection)store.Certificates;
            X509Certificate2Collection fcollection = (X509Certificate2Collection)collection.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
            X509Certificate2Collection scollection = X509Certificate2UI.SelectFromCollection(fcollection, "Test Certificate Select", "Select a certificate from the following list to get information on that certificate", X509SelectionFlag.MultiSelection);
            Console.WriteLine("Number of certificates: {0}{1}", scollection.Count, Environment.NewLine);
            foreach (X509Certificate2 x509 in scollection)
            {
                X500DistinguishedName dname = new X500DistinguishedName(x509.SubjectName);
                Console.WriteLine("X500DistinguishedName: {0}{1}", dname.Name, Environment.NewLine);
                x509.Reset();
            }
            store.Close();
        }
        catch (CryptographicException)
        {
            Console.WriteLine("Information could not be written out for this certificate.");
        }
    }
}
Imports System.Security.Cryptography
Imports System.Security.Permissions
Imports System.IO
Imports System.Security.Cryptography.X509Certificates



Class X500Sample
   Shared msg As String
   Shared Sub Main()
    
      Try
         Dim store As New X509Store("MY", StoreLocation.CurrentUser)
         store.Open((OpenFlags.ReadOnly Or OpenFlags.OpenExistingOnly))
         Dim collection As X509Certificate2Collection = CType(store.Certificates, X509Certificate2Collection)
         Dim fcollection As X509Certificate2Collection = CType(collection.Find(X509FindType.FindByTimeValid, DateTime.Now, False), X509Certificate2Collection)
         Dim scollection As X509Certificate2Collection = X509Certificate2UI.SelectFromCollection(fcollection, "Test Certificate Select", "Select a certificate from the following list to get information on that certificate", X509SelectionFlag.MultiSelection)
     msg = "Number of certificates: " & scollection.Count & Environment.NewLine
     MsgBox(msg)
         Dim x509 As X509Certificate2
         For Each x509 In  scollection
            Dim dname As New X500DistinguishedName(x509.SubjectName)
        msg = "X500DistinguishedName: " & dname.Name & Environment.NewLine
     MsgBox(msg)
            x509.Reset()
         Next x509
         store.Close()
     Catch e As Exception
            msg = "Error: Information could not be written out for this certificate."
            MsgBox(msg)
      End Try
   End Sub
End Class

Remarks

Use this method to print Abstract Syntax Notation number One (ASN.1)-encoded data or output the information to a text box. Use the multiLine parameter to control the layout of the output.

Applies to