Config.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. namespace DeviceCenter.model
  9. {
  10. class Config
  11. {
  12. private static readonly object _lock = new object();
  13. private static Config _Instance;
  14. public string url { get; set; }
  15. public string username { get; set; }
  16. public string password { get; set; }
  17. public string token { get; set; }
  18. public List<Device> devices { get; set; }
  19. public void save()
  20. {
  21. string path = AppDomain.CurrentDomain.BaseDirectory + "config.json";
  22. File.WriteAllText(path, JsonConvert.SerializeObject(this, Formatting.Indented));
  23. }
  24. public static Config getInstance()
  25. {
  26. if (null == _Instance)
  27. {
  28. lock (_lock)
  29. {
  30. if (null == _Instance)
  31. {
  32. string path = AppDomain.CurrentDomain.BaseDirectory + "config.json";
  33. if (File.Exists(path))
  34. {
  35. Config config = JsonConvert.DeserializeObject<Config>(File.ReadAllText(path));
  36. _Instance = config;
  37. }
  38. else
  39. {
  40. Config config = new Config();
  41. config.devices = new List<Device>();
  42. config.username = "";
  43. config.password = "";
  44. config.save();
  45. _Instance = config;
  46. }
  47. }
  48. }
  49. }
  50. return _Instance;
  51. }
  52. }
  53. }