private void Bgw_DoWork(object sender, DoWorkEventArgs e)
{
fileslist = GetFiles(textBoxFileDirectory.Text, "*.*", fsw.IncludeSubdirectories).ToList();
if (bgw.CancellationPending == true)
{
e.Cancel = true;
return;
}
if (fileslist.Count > 0)
{
bgw.ReportProgress(fileslist.Count);
for (int i = 0; i < fileslist.Count; i++)
{
if (bgw.CancellationPending == true)
{
e.Cancel = true;
return;
}
FileInfo info = new FileInfo(fileslist[i]);
if (File.Exists(info.FullName))
{
dic.Add(fileslist[i], info.Length);
}
}
}
}
until now i used to make :
fileslist = GetFiles(#"D:\", "*.*", fsw.IncludeSubdirectories).ToList();
but now i wan to use the text inside the textBoxFileDirectory control the problem is that you can't use ui inside the dowork event.
I tried this :
textBoxFileDirectory.Dispatcher.Invoke(DispatcherPriority.Normal,
new Action(() => { fileslist = GetFiles(textBoxFileDirectory.Text, "*.*", fsw.IncludeSubdirectories).ToList(); }));
but this make the whole application to freeze and then to continue working when getting files from drive D but it's freezing when getting the files.
but when trying to get the files this way from C drive it's also freezing but after some time i'm getting this message :
This is the GetFiles method :
public static IEnumerable<string> GetFiles(string root, string searchPattern, bool includeSubDirectories)
{
Stack<string> pending = new Stack<string>();
pending.Push(root);
while (pending.Count != 0)
{
var path = pending.Pop();
string[] next = null;
try
{
next = Directory.GetFiles(path, searchPattern);
}
catch { }
if (next != null && next.Length != 0)
foreach (var file in next) yield return file;
try
{
if (includeSubDirectories)
{
next = Directory.GetDirectories(path);
foreach (var subdir in next) pending.Push(subdir);
}
}
catch { }
}
}
I'm trying to populate a TreeList from a list of folder path, for example:
C:\WINDOWS\addins
C:\WINDOWS\AppPatch
C:\WINDOWS\AppPatch\MUI
C:\WINDOWS\AppPatch\MUI\040C
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409
and output I want like this
├───addins
├───AppPatch
│ └───MUI
│ └───040C
├───Microsoft.NET
│ └───Framework
│ └───v2.0.50727
│ └───MUI
│ └───0409
Notice: I am using Devexpress TreeList.
This is my code:
private void PopulateTreeList(TreeList treeList, IEnumerable<string> paths, char pathSeparator)
{
TreeListNode lastNode = null;
string subPathAgg;
foreach (string path in paths)
{
subPathAgg = string.Empty;
foreach (string subPath in path.Split(pathSeparator))
{
subPathAgg += subPath + pathSeparator;
TreeListNode nodes = treeList.FindNode((node) => { return node[""].ToString() == subPath; });
if (nodes == null)
lastNode = treeList.AppendNode(new object[] { subPath }, lastNode);
else
{
if(subPathAgg== GetFullPath(nodes, "\\"))
lastNode = nodes;
else
lastNode = treeList.AppendNode(new object[] { subPath }, lastNode);
}
}
lastNode = null;
}
}
private string GetFullPath(TreeListNode node, string pathSeparator)
{
if (node == null) return "";
string result = "";
while (node != null)
{
result = node.GetDisplayText(0) + pathSeparator + result;
node = node.ParentNode;
}
return result;
}
The TreeList.FindNode performing search in all nodes. You can use TreeListNodes.FirstOrDefault method to make search only in child nodes of current node.
Here is example:
private void PopulateTreeList(TreeList treeList, IEnumerable<string> paths, char pathSeparator)
{
foreach (string path in paths)
{
TreeListNode parentNode = null;
foreach (string folder in path.Split(pathSeparator))
{
var nodes = parentNode?.Nodes ?? treeList.Nodes;
var node = nodes.FirstOrDefault(item => item[0].Equals(folder));
if (node == null)
node = treeList.AppendNode(new object[] { folder }, parentNode);
parentNode = node;
}
}
}
Here is the result:
You can run example by this code:
var paths = new[]
{
#"C:\WINDOWS\addins",
#"C:\WINDOWS\AppPatch",
#"C:\WINDOWS\AppPatch\MUI",
#"C:\WINDOWS\AppPatch\MUI\040C",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409"
};
var treeList = new TreeList() { Size = new Size(400, 250) };
var column = treeList.Columns.Add();
column.VisibleIndex = 0;
PopulateTreeList(treeList, paths, Path.DirectorySeparatorChar);
treeList.ExpandAll();
var form = new Form() { Size = new Size(500, 350) };
form.Controls.Add(treeList);
form.Show();
here nodes is a dictionary of <string, List<string>>
and childnodes is a List<string> and parentnode is a string.
Whenever I clear childnodes, the values for all the keys in the nodes get cleared.
public void processData()
{
var hcols = Input.Columns.Take(Input.Columns.Count).ToList();
var childNodes = new List<string>();
var parentNode = "Default";
for (var i= 1 ; i < hcols.Count -1 ; i++)
{
var splitString = hcols[i].Name.Split('_');
if (nodes.ContainsKey(splitString[0]))
{
childNodes.Add(splitString[1]);
}
else
{
var childNodes2 = childNodes;
if (!nodes.ContainsKey(splitString[0]) && childNodes.Count > 0)
{
nodes[parentNode] = childNodes2;
}
nodes.Add(splitString[0], null);
parentNode = splitString[0];
childNodes.Clear();
if (splitString.Length > 1) childNodes.Add(splitString[1]);
}
Array.Clear(splitString, 0, splitString.Length);
}
nodes[parentNode] = childNodes;
}
you are using the same list reference for all your keys. Instead of clearing the list, create a new one:
public void processData()
{
var hcols = Input.Columns.Take(Input.Columns.Count).ToList();
var childNodes = new List<string>();
var parentNode = "Default";
for (var i= 1 ; i < hcols.Count -1 ; i++)
{
var splitString = hcols[i].Name.Split('_');
if (nodes.ContainsKey(splitString[0]))
{
childNodes.Add(splitString[1]);
}
else
{
var childNodes2 = new List<string>(childNodes); // depending on your case, you may want a copy here too
if (!nodes.ContainsKey(splitString[0]) && childNodes.Count > 0)
{
nodes[parentNode] = childNodes2;
}
nodes.Add(splitString[0], null);
parentNode = splitString[0];
childNodes = new List<string>(); // instead of clear
if (splitString.Length > 1) childNodes.Add(splitString[1]);
}
Array.Clear(splitString, 0, splitString.Length);
}
nodes[parentNode] = childNodes;
}
This is the code which I am using, but I am always getting a blank string from the function.
How do I solve such a problem?
private string GetArrayofCheckedNodes()
{
string arrCheckedNodes = "";
ArrayList al = new ArrayList();
foreach (TreeNode node in TreeView1.Nodes)
{
if (node.Checked == true) // Checking whether a node is checked or not.
{
al.Add(node.Text);
}
}
for (int i = 0; i < al.Count; i++)
{
arrCheckedNodes += al[i].ToString() + " , ";
}
return arrCheckedNodes;
}
I'm assuming you want ALL checked nodes. TreeView1.Nodes only returns the first level so you will need to recurse down the tree. Also, you can use string.Join() to join the resultant values together.
private string GetArrayofCheckedNodes()
{
return string.Join(" , ", GetCheckedNodes(treeView1.Nodes));
}
public List<string> GetCheckedNodes(TreeNodeCollection nodes)
{
List<string> nodeList = new List<string>();
if (nodes == null)
{
return nodeList;
}
foreach (TreeNode childNode in nodes)
{
if (childNode.Checked)
{
nodeList.Add(childNode.Text);
}
nodeList.AddRange(GetCheckedNodes(childNode.Nodes));
}
return nodeList;
}
I'm trying to populate a treeview from a list of folder path, for example:
C:\WINDOWS\addins
C:\WINDOWS\AppPatch
C:\WINDOWS\AppPatch\MUI
C:\WINDOWS\AppPatch\MUI\040C
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409
with an ouput like this:
├───addins
├───AppPatch
│ └───MUI
│ └───040C
├───Microsoft.NET
│ └───Framework
│ └───v2.0.50727
│ └───MUI
│ └───0409
Notice there's no 'C:\WINDOWS\Microsoft.NET' or 'C:\WINDOWS\Microsoft.NET\Framework' in the list. I've been working on this for almost two days and there's a bunch of bug in my code. Hope I can get help from here.
Thanks.
Eric
private void Form1_Load(object sender, EventArgs e)
{
var paths = new List<string>
{
#"C:\WINDOWS\AppPatch\MUI\040C",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
#"C:\WINDOWS\addins",
#"C:\WINDOWS\AppPatch",
#"C:\WINDOWS\AppPatch\MUI",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409"
};
treeView1.PathSeparator = #"\";
PopulateTreeView(treeView1, paths, '\\');
}
private static void PopulateTreeView(TreeView treeView, IEnumerable<string> paths, char pathSeparator)
{
TreeNode lastNode = null;
string subPathAgg;
foreach (string path in paths)
{
subPathAgg = string.Empty;
foreach (string subPath in path.Split(pathSeparator))
{
subPathAgg += subPath + pathSeparator;
TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
if (nodes.Length == 0)
if (lastNode == null)
lastNode = treeView.Nodes.Add(subPathAgg, subPath);
else
lastNode = lastNode.Nodes.Add(subPathAgg, subPath);
else
lastNode = nodes[0];
}
}
}
for a linq'y version:
public static TreeNode MakeTreeFromPaths(List<string> paths, string rootNodeName = "", char separator = '/')
{
var rootNode = new TreeNode(rootNodeName);
foreach (var path in paths.Where(x => !string.IsNullOrEmpty(x.Trim()))) {
var currentNode = rootNode;
var pathItems = path.Split(separator);
foreach (var item in pathItems) {
var tmp = currentNode.Nodes.Cast<TreeNode>().Where(x => x.Text.Equals(item));
currentNode = tmp.Count() > 0 ? tmp.Single() : currentNode.Nodes.Add(item);
}
}
return rootNode;
}
ehosca answer is correcr, but there is a little problem,
when I change paths to like this
C:\WINDOWS\AppPatch\MUI\040C
D:\WIS\Microsoft.NET\Framework\v2.0.50727
E:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI
C:\WINDOWS\addins
C:\WINDOWS\AppPatch
C:\WINDOWS\AppPatch\MUI
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409
It will popoulate treeview like this.
But adding some extra code we can avoid this situation. So I changed the code in PopulateTreeView
private static void PopulateTreeView(TreeView treeView, string[] paths, char pathSeparator)
{
TreeNode lastNode = null;
string subPathAgg;
foreach (string path in paths)
{
subPathAgg = string.Empty;
foreach (string subPath in path.Split(pathSeparator))
{
subPathAgg += subPath + pathSeparator;
TreeNode[] nodes = treeView.Nodes.Find(subPathAgg, true);
if (nodes.Length == 0)
if (lastNode == null)
lastNode = treeView.Nodes.Add(subPathAgg, subPath);
else
lastNode = lastNode.Nodes.Add(subPathAgg, subPath);
else
lastNode = nodes[0];
}
lastNode = null; // This is the place code was changed
}
}
Now it works fine like this
I took your code, and it work very well,
but i made just a little modification for improving the load speed
when it is used whit a large list of files
it seems like find operation, and string operations generally are very slow
private TreeNode PopulateTreeNode2(string[] paths, string pathSeparator)
{
if (paths == null)
return null;
TreeNode thisnode = new TreeNode();
TreeNode currentnode;
char[] cachedpathseparator = pathSeparator.ToCharArray();
foreach (string path in paths) {
currentnode = thisnode;
foreach (string subPath in path.Split(cachedpathseparator))
{
if (null == currentnode.Nodes[subPath])
currentnode = currentnode.Nodes.Add(subPath, subPath);
else
currentnode = currentnode.Nodes[subPath];
}
}
return thisnode;
}
then you can use:
string[] paths = {
#"C:\WINDOWS\AppPatch\MUI\040C",
#"D:\WINDOWS\Microsoft.NET\Framework\v2.0.50727",
#"E:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI",
#"C:\WINDOWS\addins",
#"C:\WINDOWS\AppPatch",
#"C:\WINDOWS\AppPatch\MUI",
#"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\MUI\0409"
};
TreeView treeview = new TreeView();
treeview.Nodes.Add(PopulateTreeNode2(paths, "\\"));
NOTE: maybe some string sensitivity check will be needed in both solutions,
in order to prevent some folders re creations.
because some url could be pointing to the same folder on the disk
but spelled different such as:
Windows ; WinDOWs , WINDOWS
Here's some very old code I once used to create an ASP.NET treeview from code (assuming TreeView has an ID of TreeViewFolders):
protected void Page_Load(object sender, EventArgs e)
{
GenerateTreeView(#"C:\WINDOWS\");
}
private void GenerateTreeView(string rootPath)
{
GetFolders(System.IO.Path.GetFullPath(rootPath), TreeViewFolders.Nodes);
TreeViewFolders.ExpandDepth = 1;
}
// recursive method to load all folders and files into tree
private void GetFolders(string path, TreeNodeCollection nodes)
{
// add nodes for all directories (folders)
string[] dirs = Directory.GetDirectories(path);
foreach (string p in dirs)
{
string dp = p.Substring(path.Length);
nodes.Add(Node("", p.Substring(path.Length), "folder"));
}
// add nodes for all files in this directory (folder)
string[] files = Directory.GetFiles(path, "*.*");
foreach (string p in files)
{
nodes.Add(Node(p, p.Substring(path.Length), "file"));
}
// add all subdirectories for each directory (recursive)
for (int i = 0; i < nodes.Count; i++)
{
if (nodes[i].Value == "folder")
GetFolders(dirs[i] + "\\", nodes[i].ChildNodes);
}
}
// create a TreeNode from the specified path, text and type
private TreeNode Node(string path, string text, string type)
{
TreeNode n = new TreeNode();
n.Value = type;
n.Text = text;
return n;
}
private void Form2_Load(object sender, EventArgs e)
{
treeView1.CheckBoxes = true;
foreach (TreeNode node in treeView1.Nodes)
{
node.Checked = true;
}
string[] drives = Environment.GetLogicalDrives();
foreach (string drive in drives)
{
// treeView1.Nodes[0].Nodes[1].Checked = true;
DriveInfo di = new DriveInfo(drive);
int driveImage;
switch (di.DriveType)
{
case DriveType.CDRom:
driveImage = 3;
break;
case DriveType.Network:
driveImage = 6;
break;
case DriveType.NoRootDirectory:
driveImage = 8;
break;
case DriveType.Unknown:
driveImage = 8;
break;
default:
driveImage = 2;
break;
}
TreeNode node = new TreeNode(drive.Substring(0, 1), driveImage, driveImage);
node.Tag = drive;
if (di.IsReady == true)
node.Nodes.Add("...");
treeView1.Nodes.Add(node);
}
foreach (TreeNode node in treeView1.Nodes)
{
node.Checked = true;
}
}
private void treeView1_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
{
if (e.Node.Nodes.Count > 0)
{
if (e.Node.Nodes[0].Text == "..." && e.Node.Nodes[0].Tag == null)
{
e.Node.Nodes.Clear();
string[] dirs = Directory.GetDirectories(e.Node.Tag.ToString());
foreach (string dir in dirs)
{
DirectoryInfo di = new DirectoryInfo(dir);
TreeNode node = new TreeNode(di.Name, 0, 1);
node.Checked = true;
try
{
node.Tag = dir;
if (di.GetDirectories().Count() > 0)
node.Nodes.Add(null, "...", 0, 0).Checked = node.Checked;
}
catch (UnauthorizedAccessException)
{
node.ImageIndex = 12;
node.SelectedImageIndex = 12;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "DirectoryLister", MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
finally
{
node.Checked = e.Node.Checked;
e.Node.Nodes.Add(node);
}
}
}
}
}
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
button1.Enabled = false;
TreeNode node = e.Node;
bool is_checked = node.Checked;
foreach (TreeNode childNode in e.Node.Nodes)
{
childNode.Checked = e.Node.Checked;
}
treeView1.SelectedNode = node;
}
I've managed to create a tree from path list using only for cycles. And it looks like it is the simpliest answer of this question at this moment.
Use this block of code to create a tree. list is the list of your files or folders, treeView1 is your TreeView.
//Creates a tree from given path list
foreach (string path in list)
{
TreeNodeCollection nodes = treeView1.Nodes;
foreach (string path_part in path.Split('\\'))
{
//Here it adds a new node (file or folder)
if (!nodes.ContainsKey(path_part))
nodes.Add(path_part, path_part);
//Go one node deeper
nodes = nodes[path_part].Nodes;
}
}
Note - this may break if you use it with paths that start with the path separator (e.g. /home/user)
And if you want to remove common parts of your paths (or remove single parent nodes), use this block of code right after the previous one:
//This removes "single" TreeNodes (common paths)
while (treeView1.Nodes.Count == 1)
{
//This "unpacks" child TreeNodes from the only parent TreeNode
for (int i = 0; i < treeView1.Nodes[0].Nodes.Count; i++)
treeView1.Nodes.Add(treeView1.Nodes[0].Nodes[i]);
//This removes parent TreeNode
treeView1.Nodes.RemoveAt(0);
}
This will generate an empty tree if all of your paths are the same.