Click here to Skip to main content
15,900,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I tried to enumerate directories contained in a directory using DirectoryInfo.GetDirectories and recursive method. The problem comes when I enumerate directories contained in a drive (c:, d:, etc). When I referred a drive different from my project and visual studio installation drive, its work well, but, if I referred a drive that same with my project location, its always refer to Debug folder on my project folder, and if I referred a drive that same with visual studio installation drive, its always refer to a location inside visual studio installation path (I don't know the exact path). Can anyone help me with this problem?. For information, I use vs2005 installed on local disc c:, and my project folder on local disc d:.
Posted

Paste the code you are using here - someone maybe able to help you.
Are you specifying a default path? If not try setting one.
 
Share this answer
 
' Enumerate all available and ready drive(s)
VB.NET
For Each drvInfo As IO.DriveInfo In IO.DriveInfo.GetDrives
            If drvInfo.IsReady Then
                Select Case drvInfo.DriveType
                    Case IO.DriveType.Fixed
                        Dim aNode As TreeNode = New TreeNode
                        aNode.Text = IIf(drvInfo.VolumeLabel <> "", drvInfo.VolumeLabel, "Local Disk") & "(" & drvInfo.Name.Replace("\", "") & ")"
                        aNode.Tag = drvInfo.Name.Replace("\", "")
                        tvwFolders.Nodes.Add(aNode)
                        gatherSubDir(aNode, drvInfo.Name.Replace("\", ""))
                    Case IO.DriveType.Removable
                        Dim aNode As TreeNode = New TreeNode
                        aNode.Text = IIf(drvInfo.VolumeLabel <> "", drvInfo.VolumeLabel, "Removable Disk") & "(" & drvInfo.Name.Replace("\", "") & ")"
                        aNode.Tag = drvInfo.Name.Replace("\", "")
                        tvwFolders.Nodes.Add(aNode)
                        gatherSubDir(aNode, drvInfo.Name.Replace("\", ""))
                End Select
            End If
        Next

' gatherSubDir routine
Private Sub gatherSubDir(ByVal node As TreeNode, ByVal dirName As String)
        Dim dInfo As IO.DirectoryInfo = New IO.DirectoryInfo(dirName)
        For Each anInfo As IO.DirectoryInfo In dInfo.GetDirectories
            Try
                If Not anInfo.Attributes And IO.FileAttributes.Hidden Then
                    Dim aNode As TreeNode
                    aNode = New TreeNode
                    aNode.Text = anInfo.Name
                    aNode.Tag = anInfo.FullName
                    node.Nodes.Add(aNode)
                    gatherSubDir(aNode, anInfo.FullName)
                End If
            Catch ex As Exception
            End Try
        Next
    End Sub

This is how I explore drive(s) and directories. I tried it by running directly under visual studio IDE. My final purpose is create a tool to find some similar file(s) on several directory, hopefully I can cut down some meaningless spaces used.
I'm sorry for my language, cause I'm originally an Indonesian.
 
Share this answer
 
Lately, I realized the difference between "C:" and "C:\" when its passed into an instance of DirectoryInfo object. When "C:" is used, its always refer to the path of the last active directory in this drive, but when "C:\" is used, its refer to root directory in this drive.
 
Share this answer
 
>Dim Files() As String = System.IO.Directory.GetFiles("D:\", "*.* ", IO.SearchOption.AllDirectories)
       Dim Filename As String
       For Each Filename In Files
magbox(Filename)
Next
 
Share this answer
 
Comments
#realJSOP 25-Jul-10 7:01am    
He's looking for directories - not files.
I played around with this and got "access denied" exceptions on some folders which aborted the GetDirectories() method and thus returned nothing. I had to scan one folder at a time, like so:

C#
////////////////////////////////////////////////////
public class FolderInfo
{
    public string Name       { get; set; }
    public bool AccessDenied { get; set; }
}

///////////////////////////////////////////////////
public class FolderList : List<FolderInfo>
{
    public int IndexOf(string name)
    {
        int index = -1;
        for (int i = 0; i < this.Count; i++)
        {
            if (this[i].Name.ToUpper() == name.ToUpper())
            {
                index = i;
                break;
            }
        }
        return index;
    }
}

///////////////////////////////////////////////////
public partial class MyForm
{
    FolderList m_folders = new FolderList();

    public MyForm()
    {
        InitializeComponents();
        GetDirectories(@"C:\");
    }

    private void GetDirectories(string path)
    {
        if (m_folders.IndexOf(path) < 0)
        {
            m_folders.Add(new FolderInfo(){Name=path, AccessDenied=false});
        }
        try
        {
            DirectoryInfo dirInfo = new DirectoryInfo(path);
            DirectoryInfo[] folders = dirInfo.GetDirectories("*.*", SearchOption.TopDirectoryOnly);
            for (int i = 0; i < folders.Length; i++)
            {
                if (!folders[i].Name.StartsWith("$"))
                {
                    GetDirectories(folders[i].FullName);
                }
            }
        }
        catch(Exception ex)
        {
            m_folders[m_folders.IndexOf(path)].AccessDenied = true;
        }
    }
}


I realize you're working in VB, but you should be able to easily convert the code.
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900