Beginner: ContextMenu Menuitems listing Folder content


New member
Feb 27, 2014
Programming Experience
I wanted to start out by saying I've taken some programming classes but my career doesn't focus anything around programming so I don't have much experience in applicable programming projects.

What I'm wanting to do is make a SysTra menu that list the folder in a designated directory, and when you hover over one of those directories in the the MenuItem if should list the contents of the folder and allow you to click/execute the listed file.

An example would be I designate a folder at C:\Stuff in that folder is App1 & App2 when I go to my SysTra I see App1 & App2 when I hover over the menu Item I see the contents and am able to click/execute the contents. The Contents in the Stuff folder is every changing, so I wasn't certain if I would have to do something like write a class that first reads the folders in C:\Stuff and there sub content that then passes it back to the main program and builds the MenuItems dynamically?

Again I'm not proficient at C# so wasn't certain if Visual studio had something built in to do majority of the leg work or if I would have to build this from scratch. I've been glancing over HowTo: Build your own System Tray Icon application | JBKB v2 & Creating a System Tray Application with C# | ... in hopes that it might give me some ideas on a approach for this, but I guess I'm also worried that I'm trying to build something that is not possible from the get go.
If possible I'd like to be able to go 4 levels, but if only two are possible I'd I might be able to do some reorganization of the structure.
You can go as deep as you like. The ContextMenuStrip has an Opening event that is raised just before the menu is displayed. You can handle that event and populate the menu at that point. That means that its contents will be up-to-date each time you open it. You can use the Directory class to get a list of folders and files in the root folder and add a menu item for each one.

For each folder, you can add a subitem with text like "(Empty)" or the like and then each folder menu item will show an arrow to display a submenu. The menu item has a DropDownOpening event that will allow you to do basically the same thing for that folder. Each time the user opens the menu or mouses over a folder menu item you use the Directory class to get a list of files and folders and populate the menu accordingly. That will go as many levels deep as you like but it will only load one folder at a time.
I would suggest that you just worry about populating the top-level items first. Handle the Opening event of the menu and populate it with folders and files. Make sure that you can click a file and use it the way you want. I'd suggest displaying just the name in the menu item but storing the full path in the Tag property for later use. Once you've got the top level working, you can expand that to populate the other levels. Don't do anything on the lower levels until the top level is working 100%.
Top Bottom