c# read pdf as base64 code example

Example 1: encode pdf file to base64 c#

static void Main(string[] args)
        {
            String path = @"d:\test";
            List<String> base64Strings = GetBase64Strings(path, 1);

            Console.WriteLine("Press any key to continue...");
            Console.ReadKey();
        }

        static List<String> GetBase64Strings(String path,int UniqueId)
        {
            List<String> base64Strings = new List<String>();
            var files = Directory.GetFiles(path, $"{UniqueId}.*");
            if (files.Length != 0)
            {
                foreach (var filePath in files)
                {
                    Byte[] bytes = File.ReadAllBytes(filePath);
                    String base64String = Convert.ToBase64String(bytes);
                    base64Strings.Add(base64String);
                }
            }
            return base64Strings;
        }

Example 2: encode pdf file to base64 c#

byte[] GetFileContentByName(string id)
{
    var file = Directory.GetFiles("c:\\temp", $"{id}.*").FirstOrDefault();
    if(file != null)
       return File.ReadAllBytes(file);    return null;
}