/// <summary> /// At startup, reads the directory included below and then one by one, loads the template CKL files /// and puts them into the database via a Template record that is a SYSTEM template. /// </summary> /// <returns> /// No return. /// </returns> public static bool LoadTemplates() { // load the templates var path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "/templatefiles/"; // make sure there are files in here if (!Directory.Exists(path)) { return(false); } // get the list of ******Manual-xccdf.xml files in here string[] filenames = Directory.GetFiles(path, "*xccdf.xml"); string rawChecklist = ""; Template t; Settings s = new Settings(); if (string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DBTYPE")) || Environment.GetEnvironmentVariable("DBTYPE").ToLower() == "mongo") { s.ConnectionString = Environment.GetEnvironmentVariable("DBCONNECTION"); s.Database = Environment.GetEnvironmentVariable("DB"); } TemplateRepository _templateRepo = new TemplateRepository(s); // remove old ones first if (filenames.Length > 0) { _templateRepo.RemoveSystemTemplates().Wait(); } // cycle through the files try { foreach (string file in filenames) { // read in the file rawChecklist = File.ReadAllText(file); rawChecklist = rawChecklist.Replace("\t", ""); try { t = MakeTemplateSystemRecord(rawChecklist, file.Substring(file.LastIndexOf("/") + 1)); // save them to the database _templateRepo.AddTemplate(t).Wait(); Console.WriteLine("Added Template file: {0}.", file); } catch (Exception tempEx) { Console.WriteLine("Error parsing template file: {0}. {1}", file, tempEx.Message); } } } catch (Exception ex) { // log the error with the ex.Message Console.WriteLine("Error parsing template files: {0}", ex.Message); } // return success return(true); }