protected void Page_Load(object sender, EventArgs e)
{
ArrayList aList = new ArrayList();
ArrayList aList1 = new ArrayList();
ArrayList aList2 = new ArrayList();
ArrayList aList3 = new ArrayList();
ArrayList aList4 = new ArrayList();
ArrayList aList5 = new ArrayList();
aList.Add("Additional Documents/test/doc3.docx");
aList.Add("Additional Documents/test/doc3.docx");
aList.Add("Additional Documents/test/test1/test1123.docx");
aList.Add("Additional Documents/test/test1/test134.docx");
aList.Add("Additional Documents/test/test1/test156.docx");
aList.Add("Additional Documents/Apple/Cat/three/Four/Fourdoc1.docx");
aList.Add("Additional Documents/Apple/Cat/three/Four/Fourss.docx");
aList.Add("Additional Documents/Apple/Ball/balloc1.docx");
aList.Add("Additional Documents/Apple/Ball/balldoc1.docx");
aList.Add("Additional Documents/Apple/Ball/I/i.docx");
aList.Add("Additional Documents/Apple/Ball/I/ii.docx");
aList.Add("Additional Documents/Apple/Ball/I1/jj.docx");
aList.Add("Additional Documents/Apple/Ball/I2/kk.docx");
aList.Add("Additional Documents/Apple/Ball/I3/ll.docx");
aList.Add("Additional Documents/Apple/Ball/I4/MM.docx");
aList.Add("Additional Documents/1/2/3/4/444.docx");
aList.Add("Additional Documents/1/2/3/4/5644.docx");
aList.Add("Additional Documents/test/test1/a2.docx");
aList.Add("Additional Documents/Apple/Two/Twooc1.docx");
aList.Add("Additional Documents/test/fff.docx");
aList.Add("Additional Documents/test/intest/abc.docx");
aList.Add("Additional Documents/test/intest1/aaa.docx");
aList.Add("Additional Documents/test/test1/a1.docx");
aList.Add("Additional Documents/test/test1/a3.docx");
aList.Add("Additional Documents/test/test1/T/b1.docx");
aList.Add("Additional Documents/test/test1/T/b2.docx");
aList.Add("Additional Documents/test/test1/TT/bb3.docx");
aList.Add("Additional Documents/test/test1/test2/test3/test4/vvvooSiste.docx");
aList.Add("Additional Documents/test/test1/test2/test3/test4/sdsds.docx");
aList.Add("Additional Documents/test/test1/test2/test3/33.docx");
aList.Add("Additional Documents/test/test1/test2/test3/t5.docx");
#region add level sorting
foreach (var item in aList)
{
string[] List = item.ToString().Split('/'); string RootValuePath = string.Empty;
List = Array.FindAll(List, val => val != List[0]).ToArray();
if (List.Length-1>=5)
{
foreach (var itemx in List.Take(6))
{
RootValuePath += itemx.ToString() + "/";
}
aList5.Add(RootValuePath.TrimEnd('/'));
}
if (List.Length-1 == 4)
{
foreach (var itemx in List.Take(5))
{
RootValuePath += itemx.ToString() + "/";
}
aList4.Add(RootValuePath.TrimEnd('/'));
}
if (List.Length - 1 == 3)
{
foreach (var itemx in List.Take(4))
{
RootValuePath += itemx.ToString() + "/";
}
aList3.Add(RootValuePath.TrimEnd('/'));
}
if (List.Length - 1 == 2)
{
foreach (var itemx in List.Take(3))
{
RootValuePath += itemx.ToString() + "/";
}
aList2.Add(RootValuePath.TrimEnd('/'));
}
}
#endregion
#region Firth Level
aList5.Sort();
foreach (var item in aList5)
{
string[] List = item.ToString().Split('/'); string RootValuePath = string.Empty;
#region >fifth level
if (List.Length>= 5)
{
foreach (var itemx in List.Take(5))
{
RootValuePath += itemx.ToString() + "/";
}
TreeNode findNode = treeView.FindNode(RootValuePath.TrimEnd('/'));
if (findNode == null)
{
for (int i = 0; i < List.Length; i++)
{
if (!IsNodeExit(treeView, List[i].ToString()))
{
if (i == 0)
{
treeView.Nodes.Add(new TreeNode(List[i].ToString()));
}
if (i == 1)
{
treeView.FindNode(List[0].ToString()).ChildNodes.Add(new TreeNode(List[i].ToString()));
}
if (i == 2)
{
treeView.FindNode(List[0].ToString() + "/" + List[1].ToString()).ChildNodes.Add(new TreeNode(List[i].ToString()));
}
if (i == 3)
{
treeView.FindNode(List[0].ToString() + "/" + List[1].ToString() + "/" + List[2].ToString()).ChildNodes.Add(new TreeNode(List[i].ToString()));
}
if (i == 4)
{
treeView.FindNode(List[0].ToString() + "/" + List[1].ToString() + "/" + List[2].ToString() + "/" + List[3].ToString()).ChildNodes.Add(new TreeNode(List[i].ToString()));
}
if (i == 5)
{
treeView.FindNode(List[0].ToString() + "/" + List[1].ToString() + "/" + List[2].ToString() + "/" + List[3].ToString() + "/" + List[4].ToString()).ChildNodes.Add(new TreeNode(List[i].ToString()));
}
}
}
}
else
{
treeView.FindNode(List[0].ToString() + "/" + List[1].ToString() + "/" + List[2].ToString() + "/" + List[3].ToString() + "/" + List[4].ToString()).ChildNodes.Add(new TreeNode(List[5].ToString()));
}
}
#endregion
}
#endregion
#region 4th level
aList4.Sort();
foreach (var item in aList4)
{
string[] List = item.ToString().Split('/'); string RootValuePath = string.Empty;
foreach (var itemx in List.Take(4))
{
RootValuePath += itemx.ToString() + "/";
}
if (!IsNodeExit(treeView, RootValuePath.TrimEnd('/')))
{
if (!IsNodeExit(treeView, List[0].ToString()))
{
treeView.Nodes.Add(new TreeNode(List[0].ToString()));
}
if (!IsNodeExit(treeView, List[0].ToString() + "/" + List[1].ToString()))
{
treeView.FindNode(List[0].ToString()).ChildNodes.Add(new TreeNode(List[1].ToString()));
}
if (!IsNodeExit(treeView, List[0].ToString() + "/" + List[1].ToString() + "/" + List[2].ToString()))
{
treeView.FindNode(List[0].ToString() + "/" + List[1].ToString()).ChildNodes.Add(new TreeNode(List[2].ToString()));
}
if (!IsNodeExit(treeView, List[0].ToString() + "/" + List[1].ToString() + "/" + List[2].ToString() + "/" + List[2].ToString()))
{
treeView.FindNode(List[0].ToString() + "/" + List[1].ToString() + "/" + List[2].ToString()).ChildNodes.Add(new TreeNode(List[3].ToString()));
}
if (!IsNodeExit(treeView, List[0].ToString() + "/" + List[1].ToString() + "/" + List[2].ToString() + "/" + List[2].ToString()))
{
treeView.FindNode(List[0].ToString() + "/" + List[1].ToString() + "/" + List[2].ToString() + "/" + List[3].ToString()).ChildNodes.Add(new TreeNode(List[4].ToString()));
}
}
else
{
treeView.FindNode(RootValuePath.TrimEnd('/')).ChildNodes.Add(new TreeNode(List[4].ToString()));
}
}
#endregion
#region 3 level
aList3.Sort();
foreach (var item in aList3)
{
string[] List = item.ToString().Split('/'); string RootValuePath = string.Empty;
foreach (var itemx in List.Take(3))
{
RootValuePath += itemx.ToString() + "/";
}
if (!IsNodeExit(treeView, RootValuePath.TrimEnd('/')))
{
if (!IsNodeExit(treeView, List[0].ToString()))
{
treeView.Nodes.Add(new TreeNode(List[0].ToString()));
}
if (!IsNodeExit(treeView, List[0].ToString()+"/"+List[1].ToString()))
{
treeView.FindNode(List[0].ToString()).ChildNodes.Add(new TreeNode(List[1].ToString()));
}
if (!IsNodeExit(treeView, List[0].ToString() + "/" + List[1].ToString() + "/" + List[2].ToString()))
{
treeView.FindNode(List[0].ToString() + "/" + List[1].ToString()).ChildNodes.Add(new TreeNode(List[2].ToString()));
}
if (!IsNodeExit(treeView, List[0].ToString() + "/" + List[1].ToString() + "/" + List[2].ToString() +"/" + List[2].ToString()))
{
treeView.FindNode(List[0].ToString() + "/" + List[1].ToString() + "/" + List[2].ToString()).ChildNodes.Add(new TreeNode(List[3].ToString()));
}
}
else
{
treeView.FindNode(RootValuePath.TrimEnd('/')).ChildNodes.Add(new TreeNode(List[3].ToString()));
}
}
#endregion
#region 2 level
aList2.Sort();
foreach (var item in aList2)
{
string[] List = item.ToString().Split('/'); string RootValuePath = string.Empty;
foreach (var itemx in List.Take(2))
{
RootValuePath += itemx.ToString() + "/";
}
if (!IsNodeExit(treeView, RootValuePath.TrimEnd('/')))
{
if (!IsNodeExit(treeView, List[0].ToString()))
{
treeView.Nodes.Add(new TreeNode(List[0].ToString()));
}
if (!IsNodeExit(treeView, List[0].ToString() + "/" + List[1].ToString()))
{
treeView.FindNode(List[0].ToString()).ChildNodes.Add(new TreeNode(List[1].ToString()));
}
if (!IsNodeExit(treeView, List[0].ToString() + "/" + List[1].ToString() + "/" + List[2].ToString()))
{
treeView.FindNode(List[0].ToString() + "/" + List[1].ToString()).ChildNodes.Add(new TreeNode(List[2].ToString()));
}
}
else
{
treeView.FindNode(RootValuePath.TrimEnd('/')).ChildNodes.Add(new TreeNode(List[2].ToString()));
}
}
#endregion
// SortTreeNodes(treeView.Nodes);
treeView.CollapseAll();
treeView.ShowLines = true;
}
public bool IsNodeExit(TreeView trview, string text)
{
TreeNode node = treeView.FindNode(Server.HtmlEncode(text));
if (node != null)
{
return true;
}
else
{
return false;
}
}
private void sort(TreeNode node)
{
foreach (TreeNode n in node.ChildNodes)
sort(n);
try
{
TreeNode temp = null;
List<TreeNode> childs = new List<TreeNode>();
while (node.ChildNodes.Count > 0)
{
foreach (TreeNode n in node.ChildNodes)
if (temp == null || n.Text[0] < temp.Text[0])
temp = n;
node.ChildNodes.Remove(temp);
childs.Add(temp);
temp = null;
}
node.ChildNodes.Clear();
foreach (TreeNode a in childs)
node.ChildNodes.Add(a);
}
catch { }
}
private void SortTreeNodes(TreeNodeCollection treeNodes)
{
var sorted = true;
foreach (TreeNode treeNode in treeNodes)
{
SortTreeNodes(treeNode.ChildNodes);
}
do
{
sorted = true;
for (var i = 0; i < treeNodes.Count - 1; i++)
{
var treeNode1 = treeNodes[i];
var treeNode2 = treeNodes[i + 1];
if (treeNode1.Text.CompareTo(treeNode2.Text) > 0)
{
treeNodes.RemoveAt(i + 1);
treeNodes.RemoveAt(i);
treeNodes.AddAt(i, treeNode2);
treeNodes.AddAt(i + 1, treeNode1);
}
}
} while (!sorted);
}