1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Text;
- namespace CB.Data
- {
- /// <summary>
- /// 模板文件缓存
- /// </summary>
- public class TemplateFiles
- {
- private static object lockObject = new object();
- private static Dictionary<string, string> template = new Dictionary<string, string>();
- private static string rootPath = AppDomain.CurrentDomain.BaseDirectory + CB.Config.BaseConfigs.GetConfig().TemplateRootPath + "\\";
- private TemplateFiles() { }
- static TemplateFiles() { }
- /// <summary>
- /// 获取模板文件内容
- /// </summary>
- /// <param name="filePath"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 加载制定模板文件
- /// </summary>
- 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);
- }
- }
- /// <summary>
- /// 重新加载所有模板文件
- /// </summary>
- public static void ReLoadFile()
- {
- lock (lockObject)
- {
- string path;
- Dictionary<string, string> t = new Dictionary<string, string>();
- foreach (var key in template.Keys)
- {
- path = rootPath + key;
- if (File.Exists(path))
- {
- t[key] = File.ReadAllText(path, Encoding.UTF8);
- }
- }
- template = t;
- }
- }
- }
- }
|