private static string FindExistingFilename(X509Certificate2 cert, string storePath, out bool hadCandidates)
{
hadCandidates = false;
foreach (string maybeMatch in Directory.EnumerateFiles(storePath, cert.Thumbprint + PfxWildcard))
{
hadCandidates = true;
try
{
using (X509Certificate2 candidate = new X509Certificate2(maybeMatch))
{
if (candidate.Equals(cert))
{
return maybeMatch;
}
}
}
catch (CryptographicException)
{
// Contents weren't interpretable as a certificate, so it's not a match.
}
}
return null;
}