private void LoadTemplates() { Templates.Clear(); var path = IOHelper.CheckPath(GlobalConfig.ProgramRoot, "Templates"); var folders = Directory.GetDirectories(path); foreach (var folder in folders) { var cla = new TemplateClassify { Name = Path.GetFileName(folder) }; foreach (var file in IOHelper.GetAllFiles(folder, "*.lt")) { cla.Templates.Add(new TemplateConfig { Caption = Path.GetFileNameWithoutExtension(file), Classify = cla.Name, TemplatePath = file }); } Templates.Add(cla); } }
private void SaveTemplate() { if (string.IsNullOrWhiteSpace(CurrentTemplate)) { MessageBox.Show("模板内容不能为空"); return; } CurrentTemplateConfig.Template = CurrentTemplate; try { LuaTemplateParse parser = new LuaTemplateParse { Config = CurrentTemplateConfig }; parser.Compile(); } catch (Exception e) { ExtendCode = e.ToString(); TabIndex = 2; MessageBox.Show("模板解析发生错误"); return; } CurrentLua = CurrentTemplateConfig.Code; CurrentTemplate = CurrentTemplateConfig.Template; var cl = CurrentTemplateConfig.Classify; if (CurrentTemplateConfig.TemplatePath != null) { File.WriteAllText(CurrentTemplateConfig.TemplatePath, CurrentTemplateConfig.Template); } if (string.IsNullOrWhiteSpace(CurrentTemplateConfig.Name)) { MessageBox.Show("必须有一个名称(Name)"); return; } if (string.IsNullOrWhiteSpace(CurrentTemplateConfig.Caption)) { MessageBox.Show("必须有一个标题(Caption)"); return; } if (string.IsNullOrWhiteSpace(CurrentTemplateConfig.Classify)) { MessageBox.Show("必须有一个分类(Classify)"); return; } if (cl != null && cl != CurrentTemplateConfig.Classify) { var cla = Templates.FirstOrDefault(p => p.Name == cl); cla?.Templates.Remove(CurrentTemplateConfig); } var ncla = Templates.FirstOrDefault(p => p.Name == CurrentTemplateConfig.Classify); if (ncla == null) { Templates.Add(ncla = new TemplateClassify { Name = CurrentTemplateConfig.Classify }); } if (!ncla.Templates.Contains(CurrentTemplateConfig)) { ncla.Templates.Add(CurrentTemplateConfig); } string path = IOHelper.CheckPath(GlobalConfig.ProgramRoot, "Templates", CurrentTemplateConfig.Classify); path = Path.Combine(path, CurrentTemplateConfig.Caption + ".lt"); if (!string.IsNullOrWhiteSpace(CurrentTemplateConfig.TemplatePath) && !string.Equals(CurrentTemplateConfig.TemplatePath, path, StringComparison.OrdinalIgnoreCase) && File.Exists(CurrentTemplateConfig.TemplatePath)) { File.Delete(CurrentTemplateConfig.TemplatePath); } CurrentTemplateConfig.TemplatePath = path; File.WriteAllText(path, CurrentTemplateConfig.Template); }