Results 1 to 5 of 5

Thread: Moving up in a Directory Structure

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Moving up in a Directory Structure

    Hello!
    On my form I have this:
    VB Code:
    1. Private SSFolderList() As String 'folders
    2.     Private SSFileList() As String 'files
    3.     Private SSInit As String = "C:\" 'initial folder
    4.     Private SSBack As String 'to move back
    5.  
    6. 'load directories
    7.     Private Sub SSLoadDir()
    8.         SSFolderList = Directory.GetDirectories(SSInit, "*")
    9.  
    10.         For Each strTemp As String In SSFolderList
    11.             lstSSDir.Items.Add(strTemp) 'add all folders to listbox
    12.         Next
    13.     End Sub
    The above code is to load a listbox with folders. It works. Now I want to be able to move up in the directory tree, iow, If I click back - it must take me to the previous parent folder, when I click back again, it needs to move again.

    What I have so far is:
    VB Code:
    1. Private Sub butSSBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles butSSBack.Click
    2.         Dim SSSlash As Integer
    3.  
    4.         SSSlash = SSInit.LastIndexOf("\")
    5.         SSBack = SSInit.Substring(0, SSSlash + 1)
    6.  
    7.         Dim SSGoBack() As String
    8.  
    9.         SSGoBack = Directory.GetDirectories(SSBack)
    10.  
    11.         lstSSDir.Items.Clear()
    12.  
    13.         For Each strTemp As String In SSGoBack
    14.             lstSSDir.Items.Add(strTemp)
    15.  
    16.         Next
    17.  
    18.     End Sub
    But it only goes back one level - when I click back again, it doesn't go to the previous parent folder.

    I want the same effect as the Up button in the File Open / Save dialogs

    Can anyone help me please?

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,477

    Re: Moving up in a Directory Structure

    You'd need to store the full path to the current folder in a string variable. You would then get the path of the parent folder by:
    VB Code:
    1. Dim parentPath As String = IO.Path.GetDirectoryName(currentPath)
    Once you've done that you then need to make that new path the current path, which is probably what you weren't doing before. You probably just kept using the same base path to get the parent.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: Moving up in a Directory Structure

    I get the same results
    I want to go back one level each time until I reach C:\

  4. #4
    Fanatic Member dom_stapleton's Avatar
    Join Date
    Sep 2005
    Location
    Leigh-on-Sea, UK
    Posts
    665

    Re: Moving up in a Directory Structure

    I needed to do a similar thing in VB6 which seemed to work. I wrote a loop which stipped a character from the end of the path until the last character was a backslash. When this was so it used it would navigate to this new path. This seemed to work. But I am sure that, as jmcilhinney, there is a more advanced way of doing it in .NET. I'll try and hunt down the code for this if you want?
    I use Microsoft Visual Basic 2008 Express Edition.

    If my post has been helpful, please rate it, unless you don't believe in Karma... which actually I don't!

    Resources:
    Visual Basic Tutorials (1, 2) | MSDN Library | Google | Krugle | Search Forums

    Free components:
    Windows Forms Components | XP Common Controls Library

  5. #5
    No place like 127.0.0.1 eyeRmonkey's Avatar
    Join Date
    Jul 2005
    Location
    Blissful Oblivion
    Posts
    2,306

    Re: Moving up in a Directory Structure

    Quote Originally Posted by dom_stapleton
    I needed to do a similar thing in VB6 which seemed to work. I wrote a loop which stipped a character from the end of the path until the last character was a backslash. When this was so it used it would navigate to this new path. This seemed to work. But I am sure that, as jmcilhinney, there is a more advanced way of doing it in .NET. I'll try and hunt down the code for this if you want?
    At the very least you could use LastIndexOf (or in VB6 it would be InStrRev) to find the last backslash. Then you could get a substring from 0 to that the number that LastIndexOf returns. But I'm sure there is still a better way.
    Visual Studio 2005 Professional Edition (.NET Framework 2.0)
    ~ VB .NET Links: Visual Basic 6 to .NET Function Equivalents (Thread) | Refactor! (White Paper) | Easy Control for Wizard Forms | Making A Proper UI For WinForms | Graphics & GDI+ Tutorial | Websites For Free Icons
    ~ QUOTE: Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rich Cook

    ~ eyeRmonkey.com

Posting Permissions

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



Click Here to Expand Forum to Full Width