Эх сурвалжийг харах

更新 层叠结构加载和保存尝试

lintx 5 сар өмнө
parent
commit
f2153e597c

+ 115 - 0
WpfSample/Models/DemoItem.cs

@@ -0,0 +1,115 @@
+using System.IO;
+using System.Text.Json;
+
+namespace WpfSample.Models {
+    internal class DemoItem {
+        public int Id { get; set; } = 1;
+        public int Order { get; set; } = 1;
+        public string Name { get; set; } = string.Empty;
+        public string Description { get; set; } = string.Empty;
+
+        public DemoItem Parent { get; set; } = DemoRoot.Root;
+        public List<DemoItem> Items { get; } = new List<DemoItem>();
+        public List<DemoMessage> Messages { get; } = new List<DemoMessage>();
+
+        private IncrementNumberGenerator IncrementIdentify = new IncrementNumberGenerator();
+        public int NextId => IncrementIdentify.Value;
+        public int NextOrder => Items.Count;
+
+
+        static public DemoItem? Load(DemoItem parent, string path, string? name = null) {
+            if (path == null || Directory.Exists(path) == false) {
+                return null;
+            }
+
+            DirectoryInfo directoryInfo = new DirectoryInfo(path);
+            if (name == null) {
+                name = directoryInfo.Name;
+            }
+            if (parent == null) {
+                parent = DemoRoot.Root;
+            }
+
+            DemoItem data = new DemoItem() {
+                Name = name,
+                Id = parent.NextId,
+                Order = parent.NextOrder,
+                Parent = parent,
+            };
+
+            // 加载 文件
+            foreach (FileInfo messageFileInfo in directoryInfo.GetFiles("*.json")) {
+                using StreamReader reader = new StreamReader(messageFileInfo.FullName);
+                if (JsonSerializer.Deserialize<DemoMessage>(reader.ReadToEnd()) is DemoMessage message) {
+                    data.Messages.Add(message);
+                }
+            }
+
+            // 加载 子元素
+            foreach (DirectoryInfo subItemDirectoryInfo in directoryInfo.GetDirectories()) {
+                if (Load(data, subItemDirectoryInfo.FullName) is DemoItem subItem) {
+                    data.Items.Add(subItem);
+                }
+            }
+
+            return data;
+        }
+
+        static public bool Save(DemoItem data, string path, bool overwrite = false) {
+            if (path == null) {
+                return false;
+            }
+
+            if (overwrite == false && (Directory.Exists(path) || File.Exists(path))) {
+                return false;
+            }
+
+            if (overwrite == true && Directory.Exists(path)) {
+                try {
+                    Directory.Delete(path, true);
+                }
+                catch {
+                    return false;
+                }
+            }
+
+            if (overwrite == true && File.Exists(path)) {
+                try {
+                    File.Delete(path);
+                }
+                catch {
+                    return false;
+                }
+            }
+
+            try {
+                Directory.CreateDirectory(path);
+            }
+            catch {
+                return false;
+            }
+
+            // 保存 文件
+            foreach (DemoMessage message in data.Messages) {
+                string messageFilePath = Path.Combine(path, $"{message.Name}.json");
+                try {
+                    using FileStream file = File.Create(messageFilePath);
+                    JsonSerializer.Serialize(file, message);
+                }
+                catch {
+                    continue;
+                }
+            }
+
+            // 保存 子元素
+            foreach (DemoItem subItem in data.Items) {
+                string subItemPath = Path.Combine(path, subItem.Name);
+                if (Save(subItem, subItemPath, overwrite) == false) {
+
+                }
+            }
+
+            return true;
+        }
+    }
+}

+ 6 - 0
WpfSample/Models/DemoMessage.cs

@@ -0,0 +1,6 @@
+namespace WpfSample.Models {
+    internal class DemoMessage {
+        public string Name { get; set; } = string.Empty;
+        public string Value { get; set; } = string.Empty;
+    }
+}

+ 6 - 0
WpfSample/Models/DemoRoot.cs

@@ -0,0 +1,6 @@
+namespace WpfSample.Models {
+    internal class DemoRoot: DemoItem {
+        protected DemoRoot() { }
+        static public DemoRoot Root = new DemoRoot();
+    }
+}

+ 6 - 0
WpfSample/Models/IncrementNumberGenerator.cs

@@ -0,0 +1,6 @@
+namespace WpfSample.Models {
+    internal class IncrementNumberGenerator {
+        public int Value => ++value;
+        protected int value = 0;
+    }
+}