private void ShouCangMenuItem() //动态生成收藏夹菜单
{
try
{
string favorfolder = Environment.GetFolderPath(Environment.SpecialFolder.Favorites); //获取系统收藏夹路径
ListFiles(收藏夹ToolStripMenuItem, new DirectoryInfo(favorfolder));
}
catch (System.Exception ex)
{
throw ex;
}
}
public void ListFiles(ShouCangJiaMenuItem toolstripmenuitem,FileSystemInfo info) //生成收藏夹菜单的函数,递归使用
{
if (!info.Exists) return;
DirectoryInfo dir = info as DirectoryInfo;
//不是目录
if (dir == null) return;
FileSystemInfo[] files = dir.GetFileSystemInfos();
ShouCangJiaMenuItem[] ShouCangsMenuItem = new ShouCangJiaMenuItem[files.Length];
for (int i = 0; i < files.Length; i++)
{
FileInfo file = files[i] as FileInfo;
// ShouCangsMenuItem[i].AutoSize = false;
//是文件
if (file !=null)
{
ShouCangsMenuItem[i] = new ShouCangJiaMenuItem(file.Name,file.FullName);
ShouCangsMenuItem[i].Click += new EventHandler(ShouCangsMenuItem_Click);
//ShouCangsMenuItem[i].ToolTipText = GetMenuItemToolTipText(file.Name, file.FullName);
//ShouCangsMenuItem[i].AutoSize = false;
//把获得的URL赋值给菜单工具提示文本
toolstripmenuitem.DropDownItems.Add(ShouCangsMenuItem[i]);
}
//对于子目录,进行递归调用
else
{
DirectoryInfo Direct = files[i] as DirectoryInfo;
ShouCangsMenuItem[i] = new ShouCangJiaMenuItem(files[i].Name);
//ShouCangsMenuItem[i].Click += new EventHandler(ShouCangsMenuItem_Click);
toolstripmenuitem.DropDownItems.Add(ShouCangsMenuItem[i]);
ListFiles(ShouCangsMenuItem[i], new DirectoryInfo(Direct.FullName)); //递归使用,生成子菜单
}
}
}