public static bool CertificateAvailable(string basename, CertificateType certtype, StoreFormat format)
 {
     return(File.Exists(CertificatePath(basename, certtype, format)));
 }
        public static string CertificatePath(string basename, CertificateType certtype, StoreFormat format)
        {
            string filename          = "";
            string privateOutputPath = null;
            string publicOutputPath  = null;

            if (certtype == CertificateType.AuthorityCertificate)
            {
                privateOutputPath = AuthorityPrivateCertificatesPath;
                publicOutputPath  = AuthorityPublicCertificatesPath;
            }
            else if (certtype == CertificateType.ServerCertificate)
            {
                privateOutputPath = ServerPrivateCertificatesPath;
                publicOutputPath  = ServerPublicCertificatesPath;
            }
            else
            {
                privateOutputPath = UserPrivateCertificatesPath;
                publicOutputPath  = UserPublicCertificatesPath;
            }

            switch (format)
            {
            case StoreFormat.CRT:
                filename = publicOutputPath + basename + ".crt";
                break;

            case StoreFormat.P12Store:
                filename = privateOutputPath + basename + ".p12";
                break;

            case StoreFormat.PFX:
                filename = privateOutputPath + basename + ".pfx";
                break;

            default:
                throw new NotImplementedException();
            }

            return(filename);
        }
        public static X509Certificate2 LoadCertificate(string basename, string password, CertificateType certtype, StoreFormat format)
        {
            string filename          = "";
            string privateOutputPath = null;
            string publicOutputPath  = null;

            if (certtype == CertificateType.AuthorityCertificate)
            {
                privateOutputPath = AuthorityPrivateCertificatesPath;
                publicOutputPath  = AuthorityPublicCertificatesPath;
            }
            else if (certtype == CertificateType.ServerCertificate)
            {
                privateOutputPath = ServerPrivateCertificatesPath;
                publicOutputPath  = ServerPublicCertificatesPath;
            }
            else
            {
                privateOutputPath = UserPrivateCertificatesPath;
                publicOutputPath  = UserPublicCertificatesPath;
            }

            switch (format)
            {
            case StoreFormat.CRT:
                filename = publicOutputPath + basename + ".crt";
                break;

            case StoreFormat.P12Store:
                filename = privateOutputPath + basename + ".p12";
                break;

            case StoreFormat.PFX:
                filename = privateOutputPath + basename + ".pfx";
                break;

            default:
                throw new NotImplementedException();
            }

            return(new X509Certificate2(filename, password, X509KeyStorageFlags.Exportable));
        }