TreeView Control Files and Folders List for Pocket PC






3.33/5 (2 votes)
List all files and folders present in your Pocket PC in a TreeView control.
Introduction
This is a C# Pocket PC implementation of a files and folders list bound to a TreeView control.
The Tree View Control for .NET CF 1.0
The
Nodes
collection of the TreeView
control holds all the TreeNode
objects that are assigned to the TreeView
control. The tree nodes in this collection are referred to as the root tree nodes. Any tree node that is subsequently added to a root tree node is referred to as a child node. Because each TreeNode
can contain a collection of other TreeNode
objects, you might find it difficult to determine your location in the tree structure when you iterate through the collection. You can parse the TreeNode.FullPath
string by using the PathSeparator
string value to determine where a TreeNode
label begins and ends.
You can bind the collection of files and folders in the tree view control.
Using the Code
The demo collects all file and folder information present in the root directory and binds the folder and file names to the TreeView
control as nodes. It also displays the selected file and folder full path in a label.
//
// This code is called when you want to load
// the files and folders in to the Tree View Control.
//
treeViewCtr.Nodes.Clear();
TreeNode node = new TreeNode();
if (Directory.Exists(@"\"))
{
DirectoryInfo dirInfo = new DirectoryInfo(@"\");
DirectoryInfo[] subdirInfo = dirInfo.GetDirectories();
if (subdirInfo.Length > 0)
{
foreach (DirectoryInfo dri in subdirInfo)
{
node = treeViewCtr.Nodes.Add(dri.Name);
GetFiles(dri, node);
}
}
FileInfo[] fileInfo = dirInfo.GetFiles("*.*");
if (fileInfo.Length > 0)
{
for (int k = 0; k < fileInfo.Length; k++)
{
treeViewCtr.Nodes.Add(fileInfo[k].Name);
}
}
}
//
// GetFiles is a method which gets
// all the files and subfolders from a Directory info.
//
private void GetFiles(DirectoryInfo dri, TreeNode node)
{
DirectoryInfo[] dInfo = dri.GetDirectories();
if (dInfo.Length > 0)
{
TreeNode treeNode = new TreeNode();
foreach (DirectoryInfo driSub in dInfo)
{
treeNode = node.Nodes.Add(driSub.Name);
GetFiles(driSub, treeNode);
}
}
FileInfo[] subfileInfo = dri.GetFiles("*.*");
if (subfileInfo.Length > 0)
{
for (int j = 0; j < subfileInfo.Length; j++)
{
node.Nodes.Add(subfileInfo[j].Name);
}
}
}
//
// This code executed when the tree node is selested.
// Write this code in the Tree view controls After_Selected Event.
//
private void treeViewCtr_AfterSelect(object sender, TreeViewEventArgs e)
{
lblSelectedItem.Text =@"\"+ FixPath(e.Node).ToString();
lblSelectedItem.Refresh();
}
//
// The FillPath Methos CAlled the about After_Select Event
// is to get the selected file or Directory FullPath.
// FillPath() Method
public string FixPath(TreeNode treeNode)
{
string setReturn = "";
try
{
setReturn = treeNode.FullPath;
int index = setReturn.IndexOf("\\\\");
if (index > 1)
{
setReturn = treeNode.FullPath.Remove(index, 1);
}
}
catch (Exception ex)
{
string error = ex.ToString();
}
return setReturn;
}
Conclusion.
I hope this is useful to all beginners who want to know how to start programming for Pocket PC in C#. You can use this TreeView
control for implementing a variety of functionalities.