friendsterTalk - Friendster Forum

friendsterTalk - Friendster Forum

Welcome guest! Please Login or Register.

#1  2008-11-11 17:23:09

creamownedz
 active server protocol
creamownedz's display avatar
» FriendsterTalker
PsychoCoder
Class-S
Location: Ftalk & MCTF
Registered: 2008-03-22
Posts: 115
Last visit: Yesterday
Reputation: 7

Retrieve all sub directories of a directory [c#]

This is a snippet I use to retrieve all the sub-directories of a given directory. It returns the values in a single HashTable so remember the first entry is the initial root directory



Instructions: Pass the method the directory, it will get the root directory, then drill down getting all subs of the directory provided


Code:

//namespace reference
using System;
using System.IO;

/// <summary>
/// method for finding all the sub-directories for a given directory
/// </summary>
/// <param name="dir">directory to retrieve info for</param>
/// <returns></returns>
public Hashtable ListAllSubDirectories(string dir)
{
    //variable to hold our results
    Hashtable dirResults = new Hashtable();
    try
    {
        //get the root directory of the directory path provided
        Directory.GetDirectoryRoot(dir);
        //get the last location of a backslash ("\")
        int loc = dir.LastIndexOf('\\');
        //remove the last backslash
        dir = dir.Remove(loc, dir.Length - loc);
        //add the root to the HashTable
        dirResults.Add("Root Directory", dir);
        //create a string array to hold all the directories under the root
        string[] directories = Directory.GetDirectories(dir);
        foreach (string directory in directories)
        { 
            dirResults.Add("SubDirectory:",directory);
        }
    }
    catch (Exception ex)
    {
        dirResults = null;
        MessageBox.Show(ex.Message);
    }
    return dirResults;
}

...Giving of reputation are highly appreciating for me...
Computers don't make errors what they do they do on purpose
...computer is my expertise...
....an addicted coder....
....don't ripp all my tutorials....

Search Friendstertalk

Board footer

FriendsterTalk is not affiliated with Friendster.com
Copyright © 2002–2009 PunBB

[ 8 queries - 0.024 second ]

Pay Per Click Ads by pay per click advertising by Kontera

FriendsterTalk.com x

Welcome to FriendsterTalk! You'll need to login in order to fully use all the features and view all the sections of this site.

Please register if you're not yet a member. =)