X509Certificate.CreateFromCertFile(String) 方法

定義

從指定的 PKCS7 已簽署檔案建立 X.509v3 憑證。Creates an X.509v3 certificate from the specified PKCS7 signed file.

public:
 static System::Security::Cryptography::X509Certificates::X509Certificate ^ CreateFromCertFile(System::String ^ filename);
public static System.Security.Cryptography.X509Certificates.X509Certificate CreateFromCertFile (string filename);
static member CreateFromCertFile : string -> System.Security.Cryptography.X509Certificates.X509Certificate
Public Shared Function CreateFromCertFile (filename As String) As X509Certificate

參數

filename
String

要建立 X.509 憑證的來源 PKCS7 已簽署檔案的路徑。The path of the PKCS7 signed file from which to create the X.509 certificate.

傳回

新建立的 X.509 憑證。The newly created X.509 certificate.

例外狀況

filename 參數為 nullThe filename parameter is null.

範例

下列範例會從指定的認證檔案建立 X509CertificateThe following example creates an X509Certificate from a specified certification file.

using namespace System;
using namespace System::Security::Cryptography::X509Certificates;
int main()
{
   
   // The path to the certificate.
   String^ Certificate = "Certificate.cer";
   
   // Load the certificate into an X509Certificate object.
   X509Certificate^ cert = X509Certificate::CreateFromCertFile( Certificate );
   
   // Get the value.
   String^ resultsTrue = cert->ToString( true );
   
   // Display the value to the console.
   Console::WriteLine( resultsTrue );
   
   // Get the value.
   String^ resultsFalse = cert->ToString( false );
   
   // Display the value to the console.
   Console::WriteLine( resultsFalse );
}


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


public class X509
{

    public static void Main()
    {

        // The path to the certificate.
        string Certificate =  "Certificate.cer";

        // Load the certificate into an X509Certificate object.
        X509Certificate cert = X509Certificate.CreateFromCertFile(Certificate);

        // Get the value.
        string resultsTrue = cert.ToString(true);
       
        // Display the value to the console.
        Console.WriteLine(resultsTrue);

        // Get the value.
        string resultsFalse = cert.ToString(false);
       
        // Display the value to the console.
        Console.WriteLine(resultsFalse);
  
    }

}
Imports System.Security.Cryptography.X509Certificates




Public Class X509
   
   
   Public Shared Sub Main()
      
      ' The path to the certificate.
      Dim Certificate As String = "Certificate.cer"
      
      ' Load the certificate into an X509Certificate object.
      Dim cert As X509Certificate = X509Certificate.CreateFromCertFile(Certificate)
      
      ' Get the value.
      Dim resultsTrue As String = cert.ToString(True)
      
      ' Display the value to the console.
      Console.WriteLine(resultsTrue)
      
      ' Get the value.
      Dim resultsFalse As String = cert.ToString(False)
      
      ' Display the value to the console.
      Console.WriteLine(resultsFalse)
   End Sub  
End Class 

備註

Asn.1 DER 是此類別唯一支援的憑證格式。ASN.1 DER is the only certificate format supported by this class.

注意

憑證檔案不限於 .cer 檔案。The certificate file is not restricted to .cer files. 您可以使用任何 PKCS7 已簽署的檔案,包括 Authenticode 簽署的 .pfx 檔案。Any PKCS7 signed file can be used, including an Authenticode signed .pfx file.

適用於