CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Apr 2007
    Posts
    68

    Question Creating Menus based on Files and Directories

    Hi All

    I am using Visual Studios 2005 and I am creating an application for our IT Security. Part of the program is for documents and procedures. What I am trying to do is automatically create a menu based on the folders and files. So far I have it taking into account the base folder and one subfolder level and files within it, but I want it to automatically also take into account any subfolders and files that may be added at a later date without having to replicate the code below for each new level. My code is as follows, it is very messy as I am not a programmer.

    Code:
     Public Sub ISMSMenus()
            
            Dim clsMenuName As ToolStripMenuItem = CType((My.Forms.FrmMainScreen.TSMISMS), ToolStripMenuItem)
    
            Dim clsMyFiles As ToolStripMenuItem = _
                   CType(clsMenuName.DropDownItems.Add("Documents"), ToolStripMenuItem)
    
    
            '###########################################################################
    
            '////////////////// Set Lookup Directory //////////////////
            Dim MYDirectory As String
            MYDirectory = "K:\Helpdesk\Documentation\ISMS"
            '/////////////////////////////////////////////////////////
    
            '/////////////// Check for Files or Directories /////////
    
            Dim DirectoryCounter As _
                                       System.Collections.ObjectModel.ReadOnlyCollection(Of String)
            DirectoryCounter = My.Computer.FileSystem.GetDirectories(MYDirectory)
    
            Dim FileCounter As _
                                       System.Collections.ObjectModel.ReadOnlyCollection(Of String)
            FileCounter = My.Computer.FileSystem.GetFiles(MYDirectory)
    
            Dim AreDirectories As String
            Dim AreFiles As String
            If DirectoryCounter.Count = 0 Then
                AreDirectories = "False"
            Else
                AreDirectories = "True"
            End If
    
            If FileCounter.Count = 0 Then
                AreFiles = "False"
            Else
                AreFiles = "True"
            End If
            If AreDirectories = "False" And AreFiles = "False" Then
                clsMyFiles.DropDownItems.Add("No Files Added")
            End If
    
            If AreDirectories = "False" And AreFiles = "False" Then
                clsMyFiles.Visible = False
            End If
    
            Dim iFiles As Integer
            Dim iDirs As Integer
            Dim CountDirLength As Integer = MYDirectory.Length
            '/////////////////////////////////////////////////////////
    
            '////////////// Build File List //////////////////////////
            If AreFiles = "False" Then
            Else
                For iFiles = 0 To FileCounter.Count - 1
    
                    Dim MenuString As String = FileCounter.Item(iFiles).ToString
                    Dim SubCountDirLength As Integer = FileCounter.Item(iFiles).Length
                    Dim MenuSubString As String
                    Dim MenuSubStringTag As String
                    If FileCounter.Item(iFiles).ToString.EndsWith("lnk") Then
                        Dim wshShell As Object
                        wshShell = CreateObject("WScript.Shell")
                        Dim wshLink As Object
                        wshLink = wshShell.CreateShortcut(FileCounter.Item(iFiles).ToString)
                        Dim MyMenuString As String
                        Dim MyTargetString As String
                        MyMenuString = wshLink.TargetPath
                        MyTargetString = wshLink.WorkingDirectory
                        wshLink = Nothing
                        wshShell = Nothing
                        MenuSubString = Microsoft.VisualBasic.Right(MyMenuString, MyMenuString.Length - MyTargetString.Length - 1)
                    Else
                        MenuSubString = Microsoft.VisualBasic.Right(MenuString, SubCountDirLength - CountDirLength - 1)
                    End If
                    If FileCounter.Item(iFiles).ToString.EndsWith("lnk") Then
                        Dim wshShell As Object
                        wshShell = CreateObject("WScript.Shell")
                        Dim wshLink As Object
                        wshLink = wshShell.CreateShortcut(FileCounter.Item(iFiles).ToString)
                        MenuSubStringTag = wshLink.TargetPath
                        wshLink = Nothing
                        wshShell = Nothing
                    Else
                        MenuSubStringTag = FileCounter.Item(iFiles).ToString
                    End If
    
                    Dim clsFiles As ToolStripMenuItem = _
                        CType(clsMyFiles.DropDownItems.Add(MenuSubString.ToString), ToolStripMenuItem)
                    clsFiles.Tag = MenuSubStringTag.ToString
                    AddHandler clsFiles.Click, AddressOf OpenMyFile_click
                    If MenuSubStringTag.EndsWith("ppt") Then
                        clsFiles.Image = My.Resources.powerpoint
                    End If
                    If MenuSubStringTag.EndsWith("pdf") Then
                        clsFiles.Image = My.Resources.adobe
                    End If
                    If MenuSubStringTag.EndsWith("xls") Then
                        clsFiles.Image = My.Resources.excel
                    End If
                    If MenuSubStringTag.EndsWith("doc") Then
                        clsFiles.Image = My.Resources.word
                    End If
                    If MenuSubStringTag.EndsWith("html") Then
                        clsFiles.Image = My.Resources.internet
                    End If
                    If MenuSubStringTag.EndsWith("htm") Then
                        clsFiles.Image = My.Resources.internet
                    End If
                    If MenuSubStringTag.EndsWith("txt") Then
                        clsFiles.Image = My.Resources.word
                    End If
    
                Next
            End If
    
            '/////////////////////////////////////////////////////////
    
            '////////////// Build Sub Menus //////////////////////////
            If AreDirectories = "False" Then
            Else
                For iDirs = 0 To DirectoryCounter.Count - 1
                    Dim MenuString As String = DirectoryCounter.Item(iDirs).ToString
                    Dim SubCountDirLength As Integer = DirectoryCounter.Item(iDirs).Length
                    Dim MenuSubString As String
    
                    MenuSubString = Microsoft.VisualBasic.Right(MenuString, SubCountDirLength - CountDirLength - 1)
    
                    Dim clsSubitem As ToolStripMenuItem = _
                        CType(clsMyFiles.DropDownItems.Add(MenuSubString.ToString), ToolStripMenuItem)
    
                    Dim SubFileCounter As _
                                        System.Collections.ObjectModel.ReadOnlyCollection(Of String)
                    SubFileCounter = My.Computer.FileSystem.GetFiles(DirectoryCounter.Item(iDirs).ToString)
    
                    If SubFileCounter.Count = 0 Then
                        clsSubitem.DropDownItems.Add("No Files Added")
                    Else
                        Dim submenustring As String
                        For iFiles = 0 To SubFileCounter.Count - 1
                            If SubFileCounter.Item(iFiles).ToString.EndsWith("lnk") Then
                                Dim wshShell As Object
                                wshShell = CreateObject("WScript.Shell")
                                Dim wshLink As Object
                                wshLink = wshShell.CreateShortcut(SubFileCounter.Item(iFiles).ToString)
                                submenustring = wshLink.TargetPath
                                wshLink = Nothing
                                wshShell = Nothing
                            Else
                                submenustring = SubFileCounter.Item(iFiles).ToString
                            End If
    
    
                            Dim menusubfilestring As String
                            Dim SubFileCountDirLength As Integer = submenustring.Length
                            If SubFileCounter.Item(iFiles).ToString.EndsWith("lnk") Then
                                Dim wshShell As Object
                                wshShell = CreateObject("WScript.Shell")
                                Dim wshLink As Object
                                wshLink = wshShell.CreateShortcut(SubFileCounter.Item(iFiles).ToString)
                                Dim MyMenuString As String
                                Dim MyTargetString As String
                                MyMenuString = wshLink.TargetPath
                                MyTargetString = wshLink.WorkingDirectory
                                wshLink = Nothing
                                wshShell = Nothing
                                menusubfilestring = Microsoft.VisualBasic.Right(MyMenuString, MyMenuString.Length - MyTargetString.Length - 1)
                            Else
                                menusubfilestring = Microsoft.VisualBasic.Right(submenustring, SubFileCountDirLength - DirectoryCounter.Item(iDirs).ToString.Length - 1)
                            End If
    
    
                            Dim clsSubitem2 As ToolStripMenuItem = _
                        CType(clsSubitem.DropDownItems.Add(menusubfilestring.ToString), ToolStripMenuItem)
                            clsSubitem2.Tag = submenustring.ToString
                            If submenustring.EndsWith("ppt") Then
                                clsSubitem2.Image = My.Resources.powerpoint
                            End If
                            If submenustring.EndsWith("pdf") Then
                                clsSubitem2.Image = My.Resources.adobe
                            End If
                            If submenustring.EndsWith("xls") Then
                                clsSubitem2.Image = My.Resources.excel
                            End If
                            If submenustring.EndsWith("doc") Then
                                clsSubitem2.Image = My.Resources.word
                            End If
                            If submenustring.EndsWith("html") Then
                                clsSubitem2.Image = My.Resources.internet
                            End If
                            If submenustring.EndsWith("htm") Then
                                clsSubitem2.Image = My.Resources.internet
                            End If
                            If submenustring.EndsWith("txt") Then
                                clsSubitem2.Image = My.Resources.word
                            End If
    
                            AddHandler clsSubitem2.Click, AddressOf OpenMyFile_click
    
                           
    
                        Next
                    End If
                Next iDirs
            End If
    Any help is appreciated

    Regards

    Druid

  2. #2
    Join Date
    Jul 2000
    Location
    Milano, Italy
    Posts
    7,726

    Re: Creating Menus based on Files and Directories

    Here the trick: use recursion to get whatever levels could be now and in the future.

    Code:
    'in a class
    Imports System.IO
    Imports System.Windows.Forms
    
    Public Class RecurseAndMenu
       'give this the start Path,anst the toolstrip it will 
       'populate it with a root item to add to your menu
       'with all nested subitmes you need
       Public Shared Sub BuildMenuFromFileSys(ByVal StartPath As String, ByVal Tstrip As ToolStrip)
          'be sure path exists
          If Not Directory.Exists(StartPath) Then
             Throw New DirectoryNotFoundException("The path " & StartPath & " does not exist!")
          End If
    
          'if you're here, path does exist.
          'make it the root element of the search
          Dim rootDir As New DirectoryInfo(StartPath)
    
          'and of your menu
          Dim tItem As New ToolStripMenuItem(rootDir.Name)
          RecurseDirs(rootDir, tItem)
    
          'add it to your ToolStripMenu
          Tstrip.Items.Add(tItem)
    
       End Sub
    
       Public Shared Sub RecurseDirs(ByVal RootDir As DirectoryInfo, ByVal menuItm As ToolStripMenuItem)
          'add all the files of this dir
          'as subitem of the menuitem
          For Each f As FileInfo In RootDir.GetFiles()
             menuItm.DropDownItems.Add(f.Name)
          Next
          'for each subdir in this dir
          'create a new subitem and call this 
          'same function [this is recursion]
          'passing the dir and the new created subitem
          'then add the subitem to the menuitem...
          For Each subD In RootDir.GetDirectories()
             Dim tItem As New ToolStripMenuItem(subD.Name)
             RecurseDirs(subD, tItem)
             menuItm.DropDownItems.Add(tItem)
          Next
       End Sub
    End Class
    Code:
    'in your form:
    Public Class Form1
    
       Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
          RecurseAndMenu.BuildMenuFromFileSys("c:\yourPath", Me.ToolStrip1)
       End Sub
    End Class
    ...at present time, using mainly Net 4.0, Vs 2010



    Special thanks to Lothar "the Great" Haensler, Chris Eastwood , dr_Michael, ClearCode, Iouri and
    all the other wonderful people who made and make Codeguru a great place.
    Come back soon, you Gurus.

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

Featured