Click here to Skip to main content
13,249,813 members (65,521 online)
Click here to Skip to main content
Add your own
alternative version


17 bookmarked
Posted 28 Jun 2007

TreeView Control Files and Folders List for Pocket PC

, 28 Jun 2007
Rate this:
Please Sign up or sign in to vote.
List all files and folders present in your Pocket PC in a TreeView control.

Screenshot - TreeViewSample.gif


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.

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++)

//  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++)

// 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();

// 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 = "";
        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 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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Web Developer
India India
Prasanna Vignesh

You may also be interested in...


Comments and Discussions

GeneralC# program in MFC forum Pin
Abu Mami28-Jun-07 1:36
memberAbu Mami28-Jun-07 1:36 
Dead | X|
GeneralRe: C# program in MFC forum Pin
Prasannavig28-Jun-07 2:21
memberPrasannavig28-Jun-07 2:21 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 28 Jun 2007
Article Copyright 2007 by Prasannavig
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid