C# 獲取指定路徑下的文件結構(樹形結構)

 1 namespace Vue.Content
 2 { 
 3     public class FileNames
 4     {
 5         public int id { get; set; }
 6         public string text { get; set; }
 7         public state state { get; set; }
 8         public List<FileNames> children { get; set; }
 9         public string icon { get; set; }
10     }
11     public class state
12     {
13         public bool opened { get; set; }
14     }
15     //以上字段爲樹形控件中須要的屬性
16     public class GetSystemAllPath : Controller
17     {
18         //得到指定路徑下全部文件名
19         public static List<FileNames> getFileName(List<FileNames> list, string filepath)
20         {
21             DirectoryInfo root = new DirectoryInfo(filepath);
22             foreach (FileInfo f in root.GetFiles())
23             {   
24                 list.Add(new FileNames
25                 {
26                     text = f.Name,
27                     state = new state { opened = false },
28                     icon = "jstree-file"
29                 });
30             }
31             return list;
32         }
33         //得到指定路徑下的全部子目錄名
34         // <param name="list">文件列表</param>
35         // <param name="path">文件夾路徑</param>        
36         public static List<FileNames> GetallDirectory(List<FileNames> list, string path) {
37             DirectoryInfo root = new DirectoryInfo(path);
38             var dirs = root.GetDirectories();
39             if (dirs.Count()!=0) {          
40                 foreach (DirectoryInfo d in dirs)
41                 {
42                     list.Add(new FileNames
43                     {                 
44                         text = d.Name,
45                         state = new state { opened = false },
46                         children = GetallDirectory(new List<FileNames>(), d.FullName)
47                     });
48                 }
49             }
50             list = getFileName(list, path);
51             return list;
52         }       
53     }
54 }

以上爲核心部分!!!前端

 

後臺返回代碼:Sample/GetAllPathnode

[HttpGet("[action]")]
        public List<FileNames> GetAllPath() {
            //獲取當前系統的根路徑           
            string rootpath = $"{hostingEnv.ContentRootPath}/";
            var list = GetSystemAllPath.GetallDirectory(new List<FileNames>(), rootpath).ToArray();            
            return list.ToList();
        }

前臺Html:api

<div id="demo"></div>

js部分:url

 1  $('#demo')
 2             //jstree listen for event
 3             .on('changed.jstree', function (e, data) {
 4                 if (data && data.selected && data.selected.length) {                   
 5                     $('#selectpath').val(data.instance.get_path(data.selected[0], "/", 0));
 6                 } else {                    
 7                     $('#selectpath').val('Select a file from the tree.');
 8                 }               
 9             }).jstree({
10                 'core': {
11                     'data': {
12                         "url": "api/Sample/GetAllPath",
13                         "data": function (node: any) {                            
14                             return { "id": node.id };
15                         }
16                     }
17                 }
18         });

前端樹形控件使用的是jstree,具體使用方法能夠見其官網:https://www.jstree.com/spa

 

最後貼上效果圖:3d

相關文章
相關標籤/搜索