using System; using System.Collections.Generic; using System.IO; using System.Text; namespace CB.Data { /// /// 模板文件缓存 /// public class TemplateFiles { private static object lockObject = new object(); private static Dictionary template = new Dictionary(); private static string rootPath = AppDomain.CurrentDomain.BaseDirectory + CB.Config.BaseConfigs.GetConfig().TemplateRootPath + "\\"; private TemplateFiles() { } static TemplateFiles() { } /// /// 获取模板文件内容 /// /// /// public static string GetFileContent(string filePath) { //if (template.ContainsKey(filePath)) // return template[filePath]; string content = ""; lock (lockObject) { //if (template.ContainsKey(filePath)) // return template[filePath]; string path = rootPath + filePath; if (!File.Exists(path)) return ""; content = File.ReadAllText(path, Encoding.UTF8); //template[filePath] = content; } return content; } /// /// 加载制定模板文件 /// public static void LoadFile(string filePath) { lock (lockObject) { string path = rootPath + filePath; string content = ""; if (File.Exists(path)) { content = File.ReadAllText(rootPath + filePath, Encoding.UTF8); } if (template.ContainsKey(filePath)) template[filePath] = content; else template.Add(filePath, content); } } /// /// 重新加载所有模板文件 /// public static void ReLoadFile() { lock (lockObject) { string path; Dictionary t = new Dictionary(); foreach (var key in template.Keys) { path = rootPath + key; if (File.Exists(path)) { t[key] = File.ReadAllText(path, Encoding.UTF8); } } template = t; } } } }