dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: GetDirectories mistakenly returns [Documents and Settings] in Visual Studio 2012

  1. #1
    Join Date
    May 2019
    Location
    Michigan
    Posts
    16

    Unhappy GetDirectories mistakenly returns [Documents and Settings] in Visual Studio 2012

    I have a project I am working on for a friend.
    The project contains a form with:

    1 - ListBox (named "drivesList")
    1 - ListBox (named "fileList")
    1 - TreeView (named "TreeView1")

    Here is my code:
    Code:
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            
             fileList.Items.Clear()
    
            For Each di As IO.DriveInfo In DriveInfo.GetDrives
                drivesList.Items.Add(di)
            Next
    
    End Sub
    
     Private Sub drivesList_SelectedIndexChanged(sender As Object, e As EventArgs) Handles drivesList.SelectedIndexChanged
    
            TreeView1.Nodes.Clear()
    
            Try
                Dim drive As DriveInfo = DirectCast(drivesList.SelectedItem, DriveInfo)
    
                fileList.Items.Clear()
    
                For Each file As String In IO.Directory.GetFiles(drive.Name)
                    If file.Contains(drive.Name) Then file = file.Replace(drive.Name, "")
                    fileList.Items.Add(file)
                Next
    
                For Each dirinfo As DirectoryInfo In drive.RootDirectory.GetDirectories()
                    Dim dirnode = New TreeNode(dirinfo.Name)
                    Dim tmpPath As String = dirinfo.FullName.ToString
                    Dim dc As Integer = Directory.GetDirectories(tmpPath).Count
    
                    TreeView1.Nodes.Add(dirnode)
    
                    If dc <> 0 Then
                        ' add a blank "b" node so that a '+' appears before the parent node
                        dirnode.Nodes.Add("b")
                    End If
    
                Next
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
    
    End Sub
    As you can see, when the form loads, the drivesList gets populated with all of the drives it can properly access.
    When I click on a drive (ie: C:\) the SelectedIndexChanged event begins to add all of the directories of the selected drive.
    With respect to this drive, I have 10 directories on the C:\ drive (see image). None of them are {Documents and Settings} but my code keeps trying to add this folder. Can someone please tell me why?
    Name:  C drive.jpg
Views: 119
Size:  17.8 KB

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,209

    Re: GetDirectories mistakenly returns [Documents and Settings] in Visual Studio 2012

    I suspect the drive.RootDirectory call is giving you the unwanted value. To find out, use the debugger. Put a break point on the line
    Code:
    For Each dirinfo As DirectoryInfo In drive.RootDirectory.GetDirectories()
    When the break point hits, use the mouse to hover over the drive.RootDirectory property. I'm guessing this will show "Documents and Settings".

  3. #3
    Join Date
    May 2019
    Location
    Michigan
    Posts
    16

    Re: GetDirectories mistakenly returns [Documents and Settings] in Visual Studio 2012

    You are correct. This happens right after it gets the directory {DELL}. It works perfectly right up to that point.
    I did not come up with this code, I copied it off of a YouTube video. The funny thing is that I had an earlier version of this project that worked fine for getting the directories, but failed in another area. This forced me to change this code to what it is to get everything working smoothly. Now the question is, what do I do about this? Any ideas?
    Last edited by oldGMtireman; November 13th, 2019 at 09:53 AM. Reason: learned even more about error

  4. #4
    Join Date
    May 2019
    Location
    Michigan
    Posts
    16

    Re: GetDirectories mistakenly returns [Documents and Settings] in Visual Studio 2012

    I have learned more: I looked at the earlier version that I completed and remembered that I had been adding the folders to another ListBox. Just for fun, I replaced my current code with that of the earlier version changing the destination of the folders to the TreeView. It worked fine.
    Then I began to add back some of the old code running a debug with each section I added. When I added the 2 Dim lines with tmpPath and dc back, the program would error. Then I really got to looking at what the code was returning when it ran successfully. It is recursing the directories! I only have 10 top level directories on my C drive. The Documents and Settings folder is in the Users directory. What do I need to do to prevent this?

    1:21 PM 11/12/19 Ok, I have learned even more. It seems as if the GetDirectories function is working using some old DOS type code structure. It gets everything located on the drive specified to search. This means it will get '.lnk' files if it can parse them to folders which is what you get if you directory c [documents and settings] on a command prompt. It's getting other hidden folders and links too. Has anyone heard about any way to prevent this? I have attempted to do a 'TopLevelSearch' but that don't work. I even attempted to put a search string consisting of the selected drive.text (ToString) and that throws an error. I thought I could skip over anything that wasn't really located on the drive selected, but that fails too. Am I out of luck? Would someone please advise!

  5. #5
    Join Date
    May 2019
    Location
    Michigan
    Posts
    16

    Re: GetDirectories mistakenly returns [Documents and Settings] in Visual Studio 2012

    I'm not sure where I am supposed to be saying this, but I have noticed on 11/15/2019 at 7:04 AM that 164 of you have looked at this post. Did this stump all of you or are you too mad at me to even say "HI" and take a crack at trying to solve my problem. I don't know what I did to anger (to be nice) every person on the web at each and every one of theses "support sites". All I want to do is TRY to create something helpful for somebody (anybody) and have some fun doing it. It's all I have left to have fun with in this world - NO JOKE!! It's either I try having fun with this or get put 6 feet under.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,973

    Re: GetDirectories mistakenly returns [Documents and Settings] in Visual Studio 2012

    Hi. I looked at it, as I look at every post, but I'm C++ not VB so sorry, but no reply. I guess the same situation also applies to others whom have looked.

    I don't think there are many active VB gurus on this site. You would probably receive more feedback if you posted to CodeGuru's sister site http://www.vbforums.com/ which has much more active VB forums.
    Last edited by 2kaud; November 15th, 2019 at 08:17 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.3)

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,209

    Re: GetDirectories mistakenly returns [Documents and Settings] in Visual Studio 2012

    Quote Originally Posted by oldGMtireman View Post
    I'm not sure where I am supposed to be saying this, but I have noticed on 11/15/2019 at 7:04 AM that 164 of you have looked at this post. Did this stump all of you or are you too mad at me to even say "HI" and take a crack at trying to solve my problem. I don't know what I did to anger (to be nice) every person on the web at each and every one of theses "support sites". All I want to do is TRY to create something helpful for somebody (anybody) and have some fun doing it. It's all I have left to have fun with in this world - NO JOKE!! It's either I try having fun with this or get put 6 feet under.
    I doubt you angered anyone but you have to realize that a forum such as CG is made up of volunteers. So folks that know the answer and have time to answer usually will.

    As for me, you kind of lost me with the mention of DOS type code. Since you didn't post that code, I have no idea of wuat the problem is.

    At any rate, all you need is the VB.Net equivalent of Directory.GetDirectories() method (I posted C# syntax). To get the top level folders for a drive, you pass in the root drive like
    Code:
    "d:\"
    and set the option for searching top level folders only.

    C# syntax is
    Code:
    foreach(var directory in Directory.GetDirectories(@"d:\", "*", SearchOption.TopDirectoryOnly)
    {
      Console.WriteLine(directory.Name); // displays the top level directories for d:
    }
    Last edited by Arjay; November 15th, 2019 at 09:40 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)