TemplateFiles.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. namespace CB.Data
  6. {
  7. /// <summary>
  8. /// 模板文件缓存
  9. /// </summary>
  10. public class TemplateFiles
  11. {
  12. private static object lockObject = new object();
  13. private static Dictionary<string, string> template = new Dictionary<string, string>();
  14. private static string rootPath = AppDomain.CurrentDomain.BaseDirectory + CB.Config.BaseConfigs.GetConfig().TemplateRootPath + "\\";
  15. private TemplateFiles() { }
  16. static TemplateFiles() { }
  17. /// <summary>
  18. /// 获取模板文件内容
  19. /// </summary>
  20. /// <param name="filePath"></param>
  21. /// <returns></returns>
  22. public static string GetFileContent(string filePath)
  23. {
  24. //if (template.ContainsKey(filePath))
  25. // return template[filePath];
  26. string content = "";
  27. lock (lockObject)
  28. {
  29. //if (template.ContainsKey(filePath))
  30. // return template[filePath];
  31. string path = rootPath + filePath;
  32. if (!File.Exists(path))
  33. return "";
  34. content = File.ReadAllText(path, Encoding.UTF8);
  35. //template[filePath] = content;
  36. }
  37. return content;
  38. }
  39. /// <summary>
  40. /// 加载制定模板文件
  41. /// </summary>
  42. public static void LoadFile(string filePath)
  43. {
  44. lock (lockObject)
  45. {
  46. string path = rootPath + filePath;
  47. string content = "";
  48. if (File.Exists(path))
  49. {
  50. content = File.ReadAllText(rootPath + filePath, Encoding.UTF8);
  51. }
  52. if (template.ContainsKey(filePath))
  53. template[filePath] = content;
  54. else
  55. template.Add(filePath, content);
  56. }
  57. }
  58. /// <summary>
  59. /// 重新加载所有模板文件
  60. /// </summary>
  61. public static void ReLoadFile()
  62. {
  63. lock (lockObject)
  64. {
  65. string path;
  66. Dictionary<string, string> t = new Dictionary<string, string>();
  67. foreach (var key in template.Keys)
  68. {
  69. path = rootPath + key;
  70. if (File.Exists(path))
  71. {
  72. t[key] = File.ReadAllText(path, Encoding.UTF8);
  73. }
  74. }
  75. template = t;
  76. }
  77. }
  78. }
  79. }