CConfigManager.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using System.IO;
  3. using System.Xml.Serialization;
  4. namespace ProjectManagementSystem.Config
  5. {
  6. public class CConfiManager
  7. {
  8. public static CustomConfig customConfig = new CustomConfig();
  9. private static string m_strPath = "";
  10. private static string _GlobalDataFilePath = "";
  11. public static void Init()
  12. {
  13. InitConfigPath();
  14. InitGlobalData();
  15. }
  16. private static void InitConfigPath()
  17. {
  18. //获取到文件完整路径
  19. string s = System.Reflection.Assembly.GetExecutingAssembly().Location;
  20. //获取到文件所在目录
  21. FileInfo finfo = new FileInfo(s);
  22. string BaseDirectory = finfo.DirectoryName;
  23. m_strPath = Path.Combine(BaseDirectory, "PMS", "Config");
  24. _GlobalDataFilePath = Path.Combine(m_strPath, "CConfigSetting.xml");
  25. }
  26. private static void ExsitConfigPath()
  27. {
  28. DirectoryInfo dir = new DirectoryInfo(m_strPath);
  29. if (!dir.Exists)
  30. {
  31. dir.Create();
  32. }
  33. }
  34. public static void InitGlobalData()
  35. {
  36. //反序列化
  37. try
  38. {
  39. ExsitConfigPath();
  40. XmlSerializer serializer = new XmlSerializer(customConfig.GetType());
  41. if (!File.Exists(_GlobalDataFilePath))
  42. {
  43. SaveGlobalData();
  44. }
  45. else
  46. {
  47. using (TextReader tr = new StreamReader(_GlobalDataFilePath))
  48. {
  49. customConfig = (CustomConfig)serializer.Deserialize(tr);
  50. }
  51. }
  52. }
  53. catch (Exception e)
  54. {
  55. string strlog = e.Message;
  56. }
  57. }
  58. public static void SaveGlobalData()
  59. {
  60. try
  61. {
  62. ExsitConfigPath();
  63. XmlSerializer serializer = new XmlSerializer(customConfig.GetType());
  64. using (TextWriter tw = new StreamWriter(_GlobalDataFilePath))
  65. {
  66. serializer.Serialize(tw, customConfig);
  67. }
  68. }
  69. catch
  70. { }
  71. }
  72. }
  73. }