System.IO

To demonstrate some of the classes available in the System.IO namespace, a Directory application was built in the Directory directory. By recursing through the directory structure, you can make use of the Directory, File, FileInfo, and DirectoryInfo classes. (This sample application is available in the Directory directory.) Listing B.55 shows a code snippet that initializes a tree control with drive and directory information.

Listing B.55. Filling the TreeControl with File, Directory, and Drive Information
private void InitializeTreeControl()
{
// Initialize TreeControl.
// Populate the TreeView with data.
// First add the root node.
rootNode = new TreeNode("My Computer", (int)ImageListTypes.Computer, (int)ImageListTypes
.Computer);
directoryTree.Nodes.Add(rootNode);
TreeNodeCollection driveCollection =  rootNode.Nodes;
// Get a list of logical drives.
string [] driveNames = Directory.GetLogicalDrives();
foreach (string s in driveNames)
{
    ImageListTypes imageIndex = ImageListTypes.Computer;
    ImageListTypes selectedIndex = ImageListTypes.Computer;
    DriveType dt = (DriveType)NativeMethods.GetDriveTypeW(s);
    if (dt == DriveType.Removable)
    {
...
    }
    else
{
...
        // For each logical drive find the
        // directories and files
        PopulateTreeNode(s, driveNode.Nodes);
    }
...
}

Notice that it was necessary to drop down to P/Invoke to get the drive type. There doesn't seem to be a class or method to retrieve this information.

Next, the tree is populated on the level beneath. First, you get the names of the files using Directory.GetFiles(path), and then you get the names of the directories using Directory.GetDirectories(path). Listing B.56 shows how this is done.

Listing B.56. Filling the Subnodes of the Tree with File and Directory Information
private int PopulateTreeNode(string path, TreeNodeCollection nodeCollection)
{
    try
    {
        string [] fileNames = Directory.GetFiles(path);
        foreach (string s in fileNames)
        {
            TreeNode fileNode = new TreeNode(s.Substring (s.LastIndexOf(@"")+1),
                                             (int)ImageListTypes.File,
                                             (int)ImageListTypes.File);
            fileNode.Tag = s;
            nodeCollection.Add(fileNode);
        }
        string [] dirNames = Directory.GetDirectories(path);
        foreach (string s in dirNames)
        {
            TreeNode dirNode;
            string dir = s.Substring(s.LastIndexOf(@"")+1);
            if(dir.Equals("RECYCLER"))
                dirNode = new TreeNode(dir, (int)ImageListTypes.Recycle,
                                            (int)ImageListTypes.Recycle);
            else
                dirNode = new TreeNode(dir, (int)ImageListTypes.FolderClosed,
                                            (int)ImageListTypes.FolderOpen);
            dirNode.Tag = s;
            nodeCollection.Add(dirNode);
        }
    }
    catch(Exception e)
    {
        Debug.WriteLine(e);
        return 1;
    }
    return 0;
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.104.120