Click here to Skip to main content
Click here to Skip to main content

TreeView Control Files and Folders List for Pocket PC

, 28 Jun 2007 CPOL
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

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.

License

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

Share

About the Author

Prasannavig
Web Developer
India India
Prasanna Vignesh
www.tamilarulagam.com

Comments and Discussions

 
GeneralC# program in MFC forum PinmemberAbu Mami28-Jun-07 1:36 
GeneralRe: C# program in MFC forum PinmemberPrasannavig28-Jun-07 2:21 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 28 Jun 2007
Article Copyright 2007 by Prasannavig
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid