|
-
December 14th, 2009, 08:23 AM
#1
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|