Batch (Bulk) CAdES Digital Signature in C#

static void DigitallySignInCAdESFormat(string sourceFolder, string destFolder)
{
      CadesSignature cs = new CadesSignature("");

      //Digital signature certificate can be loaded from various sources

      //Load the certificate from Microsoft Store. 
      //The smart card or USB token certificates are usually available on Microsoft Certificate Store (start - run - certmgr.msc).
      //If the smart card certificate not appears on Microsoft Certificate Store it cannot be used by the library
      cs.DigitalSignatureCertificate = DigitalCertificate.LoadCertificate(false, DigitalCertificateSearchCriteria.CommonNameCN, "Certificate Name");

      //The smart card PIN dialog can be bypassed for some smart cards/USB Tokens. 
      //ATTENTION: This feature will NOT work for all available smart card/USB Tokens becauase of the drivers or other security measures.
      DigitalCertificate.SmartCardPin = "123456";

      cs.SignatureStandard = CadesSignatureStandard.CadesBes;

      System.IO.DirectoryInfo di;
      System.IO.FileInfo[] rgFiles;
      //get the files from the folder
      di = new System.IO.DirectoryInfo(sourceFolder);
      //select all files
      rgFiles = di.GetFiles("*.*");
      Console.WriteLine("Number of files: " + rgFiles.Length.ToString());

      foreach (FileInfo fi in rgFiles)
      {
          //for readonly files
          fi.Attributes = FileAttributes.Normal;

          //usually, the signed CAdES file should be saved with .p7s or .p7m extension
          File.WriteAllBytes(destFolder + fi.Name, cs.ApplyDigitalSignature(sourceFolder + fi.Name + ".p7m"));
      }
}

static void Main(string[] args)
{
      //usually, the signed CAdES file should be saved with .p7s or .p7m extension
      DigitallySignInCAdESFormat("d:\\source_folder\\", "d:\\destination_folder\\");
}

See also: