/// <summary> /// Initializes the tree views. /// </summary> private void BuildFolderTreeView() { string physicalRootFolder = this.Request.MapPath(GetRootFolderPath()); if (!Directory.Exists(physicalRootFolder)) { try { Directory.CreateDirectory(physicalRootFolder); } catch { // intentionally ignore the exception (we will show the Warning below) } } if (Directory.Exists(physicalRootFolder) && !HiddenFolders.Any(a => physicalRootFolder.IndexOf(a, StringComparison.OrdinalIgnoreCase) > 0)) { var sb = new StringBuilder(); sb.AppendLine("<ul id=\"treeview\">"); sb.Append(DirectoryNode(physicalRootFolder, physicalRootFolder)); sb.AppendLine("</ul>"); lblFolders.Text = sb.ToString(); upnlFolders.Update(); ListFolderContents(""); } else { nbWarning.Title = "Warning"; nbWarning.Text = "Folder does not exist: " + physicalRootFolder; nbWarning.Visible = true; } }
/// <summary> /// Builds a Directory treenode. /// </summary> /// <param name="directoryPath">The directory path.</param> /// <param name="physicalRootFolder">The physical root folder.</param> /// <returns></returns> protected string DirectoryNode(string directoryPath, string physicalRootFolder) { var sb = new StringBuilder(); DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath); string relativeFolderPath = directoryPath.Replace(physicalRootFolder, string.Empty); bool dataExpanded = hfSelectedFolder.Value.StartsWith(relativeFolderPath); bool selected = hfSelectedFolder.Value == relativeFolderPath; sb.AppendFormat("<li data-expanded='{2}' data-id='{0}'><span class='{3}'> {1}</span> \n", HttpUtility.HtmlEncode(relativeFolderPath), directoryInfo.Name, dataExpanded.ToTrueFalse().ToLower(), selected ? "selected" : string.Empty); try { List <string> subDirectoryList = Directory.GetDirectories(directoryPath).OrderBy(a => a).ToList(); if (subDirectoryList.Any()) { sb.AppendLine("<ul>"); foreach (var subDirectoryPath in subDirectoryList) { if (!HiddenFolders.Any(a => subDirectoryPath.IndexOf(a, StringComparison.OrdinalIgnoreCase) > 0)) { sb.Append(DirectoryNode(subDirectoryPath, physicalRootFolder)); } } sb.AppendLine("</ul>"); } sb.AppendLine("</li>"); return(sb.ToString()); } catch (Exception ex) { ShowErrorMessage(ex, string.Format("Unable to access folder {0}. Contact your administrator.", directoryPath)); return(string.Empty); } }