AppSettingService.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. namespace ST_CUBE_MES.Service
  10. {
  11. public class AppSettingService
  12. {
  13. private static AppSettingService _Instance;
  14. public static AppSettingService Instance
  15. {
  16. get
  17. {
  18. if (_Instance == null)
  19. {
  20. _Instance = new AppSettingService();
  21. }
  22. return _Instance;
  23. }
  24. }
  25. private AppSettingService()
  26. {
  27. }
  28. public string MES => appSetting?.MES;
  29. public string MechineCode => appSetting?.MechineCode;
  30. public bool LOCK => appSetting == null ? false : appSetting.LOCK;
  31. public string STCubeCliPath => appSetting?.STCubeCliPath;
  32. public string BinPath => appSetting?.BinPath;
  33. public string Port => appSetting?.Port;
  34. public string CustomDefaultOptions => appSetting?.CustomDefaultOptions;
  35. private const string settingFileName = "Settings.ini";
  36. private AppSetting appSetting;
  37. public void Load()
  38. {
  39. if (!File.Exists(settingFileName))
  40. {
  41. appSetting = new AppSetting()
  42. {
  43. MES = "phv",
  44. };
  45. return;
  46. }
  47. try
  48. {
  49. appSetting = JsonConvert.DeserializeObject<AppSetting>(File.ReadAllText(settingFileName));
  50. }
  51. catch
  52. {
  53. MessageBox.Show($"{settingFileName} format ERROR");
  54. }
  55. }
  56. public void Save(AppSetting setting)
  57. {
  58. if (File.Exists(settingFileName))
  59. {
  60. File.Delete(settingFileName);
  61. }
  62. File.WriteAllText(settingFileName, JsonConvert.SerializeObject(setting));
  63. Load();
  64. }
  65. }
  66. public class AppSetting
  67. {
  68. public string MES { get; set; }
  69. public string MechineCode { get; set; }
  70. public bool LOCK { get; set; }
  71. public string STCubeCliPath { get; set; }
  72. public string BinPath { get; set; }
  73. public string Port { get; set; }
  74. public string CustomDefaultOptions { get; set; }
  75. }
  76. }