程序文件菜单文件夹! [英] Program files menu folder!
问题描述
hi..i想在弹出菜单中列出所有启动菜单文件和文件夹!!
这很难吗?
我尝试了什么:
我在google中找不到这个!!!
我知道我可能要搜索文件夹,然后应用命令,以便菜单中的目录列表!好吧,我不知道该怎么做!!!
谢谢!
i我正在使用此代码:
hi..i would like to list all startup menu files and folders in a popup menu!!
Is it hard?
What I have tried:
I am not finding much of this in google!!!
I know i might have to search for the folder and then apply commands so the dir lists in the menu!! Well i do not know very well how to do that!!!
Thanks!
i am using this code:
Dim patQ = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Dim di = New DirectoryInfo(patQ)
Dim rgFiles = di.GetFiles("*.*")
For Each fi As FileInfo In rgFiles
Pool.ContextMenuStrip2.Items.Add(fi.Name.ToString.Replace("\" & fi.Name, ""))
Next
这列出了My Documen中的所有文件ts folder..i想要在popupmenu的一个aproppriated结构中列出所有文件,文件夹和子文件夹!!
this lists all files in My Documents folder..i would like to list all files, folders and subfolders in an aproppriated structure of a popupmenu!!
推荐答案
试试这个:
Try this:
string startMenu = Environment.GetFolderPath(Environment.SpecialFolder.CommonStartMenu);
(这是在.NET 4.0,所以如果您使用的是较低版本,则需要使用Win32 API SHGetSpecialFolderPath函数(Windows) [ ^ ]这将开发更多的工作和DllImport:
(this was added at .NET 4.0, so if you are using a lower version, you will need to use the Win32 API SHGetSpecialFolderPath function (Windows)[^] which will invlove more work and a DllImport:
[DllImport("shell32.dll")]
static extern bool SHGetSpecialFolderPath(IntPtr hwndOwner,
[Out] StringBuilder lpszPath, int nFolder, bool fCreate);
在这里...将StartMenu加载到弹出菜单中......
Here you go... Loading the StartMenu into a popup menu...
Public Class Form1
Public Sub New()
InitializeComponent()
BuildMenu()
End Sub
Private Sub BuildMenu()
Dim cm = New ContextMenu()
Dim startMenu = Environment.GetFolderPath( _
Environment.SpecialFolder.CommonStartMenu)
BuildMenuLevel(cm, startMenu)
butStartMenu.ContextMenu = cm
End Sub
' walk through the folder levels and build a menu
Private Sub BuildMenuLevel(cm As ContextMenu, parentFolder As String, _
Optional parentMenu As MenuItem = Nothing)
Dim folders = IO.Directory.GetDirectories(parentFolder)
For Each folder As String In folders
Dim files = IO.Directory.GetFiles(folder)
Dim menuItems = New List(Of MenuItem)()
For Each file As String In files
menuItems.Add(New MenuItem(_
IO.Path.GetFileNameWithoutExtension(file), _
New EventHandler(AddressOf AppClicked)))
Next
Dim folderMenu As MenuItem
Dim folderName As String = folder.Split("\"c).Last()
If parentMenu Is Nothing Then
folderMenu = cm.MenuItems.Add(folderName, menuItems.ToArray())
Else
folderMenu = parentMenu.MenuItems.Add(folderName, menuItems.ToArray())
End If
BuildMenuLevel(cm, folder, folderMenu)
Next
End Sub
Private Sub butStartMenu_Click(sender As Object, e As EventArgs) _
Handles butStartMenu.Click
butStartMenu.ContextMenu.Show(butStartMenu, _
New Point(butStartMenu.Left, butStartMenu.Top))
End Sub
Private Sub AppClicked(sender As Object, e As EventArgs)
' selected item
Dim menuItem = TryCast(sender, MenuItem)
Debug.WriteLine("File: {0}", menuItem.Text)
Debugger.Break()
End Sub
End Class
更新:如何概括BuildMenu函数以使用任何按钮&和目录文件夹:
UPDATE: How to generalize the BuildMenu function to work with any button & and directory folder:
Private Sub BuildMenu(startMenu as String, destButton as Button)
Dim cm = New ContextMenu()
BuildMenuLevel(cm, startMenu)
destButton.ContextMenu = cm
End Sub
使用:
To Use:
dim startMenuPath as String = Environment.GetFolderPath( _
Environment.SpecialFolder.CommonStartMenu)
BuildMenu(startMenuPath, butStartMenu)
这篇关于程序文件菜单文件夹!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!