DemoItem.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System.IO;
  2. using System.Text.Json;
  3. namespace WpfSample.Models {
  4. internal class DemoItem {
  5. public int Id { get; set; } = 1;
  6. public int Order { get; set; } = 1;
  7. public string Name { get; set; } = string.Empty;
  8. public string Description { get; set; } = string.Empty;
  9. public DemoItem Parent { get; set; } = DemoRoot.Root;
  10. public List<DemoItem> Items { get; } = new List<DemoItem>();
  11. public List<DemoMessage> Messages { get; } = new List<DemoMessage>();
  12. private IncrementNumberGenerator IncrementIdentify = new IncrementNumberGenerator();
  13. public int NextId => IncrementIdentify.Value;
  14. public int NextOrder => Items.Count;
  15. static public DemoItem? Load(DemoItem parent, string path, string? name = null) {
  16. if (path == null || Directory.Exists(path) == false) {
  17. return null;
  18. }
  19. DirectoryInfo directoryInfo = new DirectoryInfo(path);
  20. if (name == null) {
  21. name = directoryInfo.Name;
  22. }
  23. if (parent == null) {
  24. parent = DemoRoot.Root;
  25. }
  26. DemoItem data = new DemoItem() {
  27. Name = name,
  28. Id = parent.NextId,
  29. Order = parent.NextOrder,
  30. Parent = parent,
  31. };
  32. // 加载 文件
  33. foreach (FileInfo messageFileInfo in directoryInfo.GetFiles("*.json")) {
  34. using StreamReader reader = new StreamReader(messageFileInfo.FullName);
  35. if (JsonSerializer.Deserialize<DemoMessage>(reader.ReadToEnd()) is DemoMessage message) {
  36. data.Messages.Add(message);
  37. }
  38. }
  39. // 加载 子元素
  40. foreach (DirectoryInfo subItemDirectoryInfo in directoryInfo.GetDirectories()) {
  41. if (Load(data, subItemDirectoryInfo.FullName) is DemoItem subItem) {
  42. data.Items.Add(subItem);
  43. }
  44. }
  45. return data;
  46. }
  47. static public bool Save(DemoItem data, string path, bool overwrite = false) {
  48. if (path == null) {
  49. return false;
  50. }
  51. if (overwrite == false && (Directory.Exists(path) || File.Exists(path))) {
  52. return false;
  53. }
  54. if (overwrite == true && Directory.Exists(path)) {
  55. try {
  56. Directory.Delete(path, true);
  57. }
  58. catch {
  59. return false;
  60. }
  61. }
  62. if (overwrite == true && File.Exists(path)) {
  63. try {
  64. File.Delete(path);
  65. }
  66. catch {
  67. return false;
  68. }
  69. }
  70. try {
  71. Directory.CreateDirectory(path);
  72. }
  73. catch {
  74. return false;
  75. }
  76. // 保存 文件
  77. foreach (DemoMessage message in data.Messages) {
  78. string messageFilePath = Path.Combine(path, $"{message.Name}.json");
  79. try {
  80. using FileStream file = File.Create(messageFilePath);
  81. JsonSerializer.Serialize(file, message);
  82. }
  83. catch {
  84. continue;
  85. }
  86. }
  87. // 保存 子元素
  88. foreach (DemoItem subItem in data.Items) {
  89. string subItemPath = Path.Combine(path, subItem.Name);
  90. if (Save(subItem, subItemPath, overwrite) == false) {
  91. }
  92. }
  93. return true;
  94. }
  95. }
  96. }