public object GetWebSiteFileNodes(string root) { string rootPath = null; if( string.IsNullOrEmpty(root) == false ) { string configFile = this.MvcRuntime.GetPhysicalPath("CodeExplorer.config"); if( File.Exists(configFile) ) { Dictionary<string, string> settings = (from line in File.ReadAllLines(configFile) where line.Trim().StartsWith(";", StringComparison.Ordinal) == false let p = line.IndexOf('=') where p > 0 select new KeyValuePair<string, string>( line.Substring(0, p).Trim(), line.Substring(p + 1).Trim())) .ToDictionary(x => x.Key, y => y.Value, StringComparer.OrdinalIgnoreCase); settings.TryGetValue(root, out rootPath); } } if( string.IsNullOrEmpty(rootPath) ) // 显示整个解决方案下的所有文件。 rootPath = this.MvcRuntime.GetWebSitePath() + "..\\"; JsTreeNode rootNode = new JsTreeNode(); RecursiveDirectory(rootPath, rootNode); return new JsonResult(rootNode.children); }
private void RecursiveDirectory(string fullPath, JsTreeNode root) { DirectoryInfo dir = new DirectoryInfo(fullPath); DirectoryInfo[] subDirArray = dir.GetDirectories(); FileInfo[] fileArray = dir.GetFiles(); List<JsTreeNode> children = new List<JsTreeNode>(subDirArray.Length + fileArray.Length); foreach( DirectoryInfo dinfo in subDirArray ) { if( Array.Find<string>(s_IgnoreFolders, x => string.Compare(x, dinfo.Name, true) == 0) != null ) continue; JsTreeNode node = new JsTreeNode(); node.text = dinfo.Name; node.attributes = new JsTreeNodeCustAttr(); RecursiveDirectory(dinfo.FullName, node); if( node.children != null && node.children.Count > 0 ) { children.Add(node); } } Dictionary<string, JsTreeNode> nestNodes = new Dictionary<string, JsTreeNode>(fileArray.Length, StringComparer.OrdinalIgnoreCase); foreach( FileInfo finfo in fileArray ) { //if( Array.Find<string>(s_IgnoreFiles, // x => (string.Compare(x, finfo.Extension, true) == 0 ||string.Compare(x, finfo.Name, true) == 0) ) != null ) // continue; if( Array.Find<string>(s_AllowExts, x => (string.Compare(x, finfo.Extension, true) == 0)) == null ) continue; JsTreeNode node = new JsTreeNode(); node.text = finfo.Name; node.iconCls = GetIconByFileName(finfo.Name); node.attributes = new JsTreeNodeCustAttr(finfo.FullName, finfo.Extension.ToLower()); if( node.iconCls == "icon-cs2" ) nestNodes.Add(finfo.Name, node); else children.Add(node); } foreach( JsTreeNode node in children ) { if( node.iconCls == "icon-aspx" || node.iconCls == "icon-ascx" || node.iconCls == "icon-master" ) { JsTreeNode nestNode; if( nestNodes.TryGetValue(string.Concat(node.text, ".cs"), out nestNode) ) { node.children = new List<JsTreeNode>(1); node.children.Add(nestNode); } } } foreach( JsTreeNode child in children ) if( child.children != null && child.children.Count > 0 ) child.state = "closed"; if( children.Count > 0 ) root.children = children; }