public ETemplate addTemplate(string templatePath, string newName) { if (templatePath.StartsWith(rootName) || !Path.IsPathRooted(templatePath)) { templatePath = HttpContext.Current.Server.MapPath(templatePath); } ETemplate template = new ETemplate(); if (Path.HasExtension(newName)) { if (Path.HasExtension(templatePath)) { template.TemplatePath = Path.Combine(Path.GetDirectoryName(templatePath), newName); } else { template.TemplatePath = Path.Combine(templatePath, newName); } template.IsDirectory = false; //文件 using (FileStream fs = new FileStream(template.TemplatePath, FileMode.Create, FileAccess.Write)) { using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8)) { sw.Write(""); } } } else { template.IsDirectory = true; template.TemplatePath = Path.Combine(templatePath, newName); //文件夹 Directory.CreateDirectory(template.TemplatePath); } template.TemplateName = newName; return template; }
public ETemplate CopyTemplate(string templatePath, string newName) { if (templatePath.StartsWith(rootName) || !Path.IsPathRooted(templatePath)) { templatePath = HttpContext.Current.Server.MapPath(templatePath); } var destTemplatePath = templatePath.Replace(Path.GetFileName(templatePath), newName); File.Copy(templatePath, destTemplatePath, true); ETemplate template = new ETemplate() { IsDirectory = false, TemplateName = newName, TemplatePath = destTemplatePath }; return template; }
private void GetTemplateTree(string ext, ETemplate root) { DirectoryInfo dir = new DirectoryInfo(root.TemplatePath); DirectoryInfo[] childrens = dir.GetDirectories(); if (childrens != null && childrens.Length > 0) { if (root.Children == null) root.Children = new List<ETemplate>(); foreach (var item in childrens) { if (exceptDirs.Contains(item.Name)) continue; root.Children.Add(new ETemplate() { TemplatePath = item.FullName, TemplateName = item.Name, IsDirectory = true }); } } FileInfo[] fs = this.GetFiles(root.TemplatePath, ext); if (fs != null && fs.Length > 0) { if (root.Children == null) root.Children = new List<ETemplate>(); foreach (var item in fs) { root.Children.Add(new ETemplate() { TemplatePath = item.FullName, TemplateName = item.Name, IsDirectory = false }); } } if (root.Children != null) { foreach (var item in root.Children) { if (Directory.Exists(item.TemplatePath)) { GetTemplateTree(ext, item); } } } }
public ETemplate RenameTemplate(string templatePath, string newName) { if (templatePath.StartsWith(rootName) || !Path.IsPathRooted(templatePath)) { templatePath = HttpContext.Current.Server.MapPath(templatePath); } ETemplate template = new ETemplate(); var destTemplatePath = templatePath.Replace(Path.GetFileName(templatePath), newName); if (!Path.GetExtension(templatePath).HasValue()) { Directory.Move(templatePath, destTemplatePath); template.IsDirectory = true; } else { File.Move(templatePath, destTemplatePath); template.IsDirectory = false; } template.TemplateName = Path.GetFileName(destTemplatePath); template.TemplatePath = destTemplatePath; return template; }
public ETemplate MoveTemplate(string templatePath, string toTemplatePath) { if (templatePath.StartsWith(rootName) || !Path.IsPathRooted(templatePath)) { templatePath = HttpContext.Current.Server.MapPath(templatePath); } if (toTemplatePath.StartsWith(rootName) || !Path.IsPathRooted(toTemplatePath)) { toTemplatePath = HttpContext.Current.Server.MapPath(toTemplatePath); } if (Path.GetExtension(toTemplatePath).HasValue()) { toTemplatePath = Path.GetDirectoryName(toTemplatePath); } toTemplatePath = Path.Combine(toTemplatePath, Path.GetFileName(templatePath)); ETemplate template = new ETemplate(); if (!Path.GetExtension(templatePath).HasValue()) { template.IsDirectory = true; //移动文件夹 Directory.Move(templatePath, toTemplatePath); } else { template.IsDirectory = false; //移动文件 File.Move(templatePath, toTemplatePath); } template.TemplateName = Path.GetFileName(toTemplatePath); template.TemplatePath = toTemplatePath; return template; }
public ETemplate GetTemplateTree(string ext, string path) { string rootPath = HttpContext.Current.Server.MapPath(@"\"); rootPath = path.HasValue() ? Path.Combine(rootPath, path) : rootPath; ETemplate root = new ETemplate() { TemplateName = "模板", TemplatePath = rootPath }; this.GetTemplateTree(ext, root); return root; }