by Prasanna Vignesh
4. June 2007 10:20
Article_src.zip (426.51 kb)
Article_demo.zip (385.58 kb)
Introduction
This is a C# - Pocket Pc implementation of Files And Folders List binded in a Tree View Control.
The Tree View Control for Pocket Pc .Net CF 1.0
Nodes collection 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 & folders in the tree view control.
Using the code
In the demo, Its collects all files and folders information present
in the root directory and binds the folders and files name in the tree
view contol as nodes. And also get the selected files & folders
full path in a lable.
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);
}
}
}
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);
}
}
}
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;
}
I hope this is a useful to all the beginers how
starts programming for Pocket Pc in C#. We can use this tree view
control for different functionalities.
8e4aa0c6-fce4-4c1e-8718-38e29a026a4c|0|.0
Tags:
PocketPC