将用户快捷方式添加到文件/URL,以应用菜单






4.20/5 (2投票s)
.NET 类,用于加载文件/URL 列表,并在 Windows 窗体应用程序的菜单中显示它们
引言
UserMenuShortcuts
是一个用于 Windows 窗体的 .NET 类,它在菜单中显示文件或 URL 列表。当用户单击菜单中的项目时,该文件/URL 将在其机器上启动(例如 Notepad.exe 或 Codeproject.com)。单击引用用户系统上文件的项目将使用 .NET 的 Process.Start()
方法使用其关联的应用程序启动该项目。如果单击的项目是 URL,则将启动用户的默认浏览器并导航到指定的 URL 地址。
存储用户菜单项的文件是 UserShortcuts.xml 文件,它存储在宿主应用程序的目录中。如果未提供/找到 UserShortcuts.xml 文件,则每当宿主应用程序运行且找不到该文件时,将自动生成该文件。
使用 UserMenuShortcuts .NET 类
要在 Windows 窗体或其他 Windows .NET 控件中使用 UserMenuShortcuts
,请将菜单项添加到窗体,该菜单项将被加载的菜单快捷方式替换,然后将 UserMenuShortcuts
类包含到项目中。将单击事件处理程序添加到占位符菜单项,然后在窗体/控件类中进行以下方法调用
private void Form1_Load(object sender, EventArgs e)
{
UserShortcuts.Load(userItemsPlacehoderToolStripMenuItem,
userItemsPlacehoderToolStripMenuItem_Click);
}
private void userItemsPlacehoderToolStripMenuItem_Click(object sender, EventArgs e)
{
//NOTE: To override this action, remove the call to "LaunchUserShortcutItem"
//and access the clicked item by:
// Convert.ToString(UserShortcuts.UserShortcutItems[sender.ToString()]);
UserShortcuts.LaunchUserShortcutItem(sender.ToString());
}
UserMenuShortcuts 类的工作原理
UserMenuShortcuts
类从 UserShortcuts.xml 文件加载菜单项的哈希表,然后创建与占位符菜单项位于同一级别的菜单项集合。哈希表项的键也用作菜单项中的显示文本,而哈希表项的值是目标文件/URL。单击菜单项时,将执行对单击菜单项文本的查找,以检索目标文件/URL,然后使用 Process.Start()
启动目标。
public class UserShortcuts
{
...
...
private static Hashtable m_UserShortcutItems = new Hashtable();
public static Hashtable UserShortcutItems
{
get { return m_UserShortcutItems; }
set { m_UserShortcutItems = value; }
}
/// <summary>
/// Reads the UserShortcutItems file and loads into the specified menu item
/// </summary>
/// <param name="mniUserShortcutsPlaceholder"></param>
/// <param name="mniUserShortcutsPlaceholder_Click"></param>
public static void Load(ToolStripMenuItem mniUserShortcutsPlaceholder,
EventHandler mniUserShortcutsPlaceholder_Click)
{
// generate a sample file if it doesn't exist
if (File.Exists(_Uri) == false)
{
GenerateSampleUserShortcutFile(_Uri);
}
XmlDocument _Document = null;
// all files are relative to the EXE file
string sOldWorkingDirectory = Directory.GetCurrentDirectory();
try
{
Directory.SetCurrentDirectory(Path.GetDirectoryName
(Application.ExecutablePath));
_Document = new XmlDocument();
_Document.Load(_Uri);
m_UserShortcutItems = new Hashtable();
m_UserShortcutItems.Clear();
XmlNode root = _Document.DocumentElement;
XPathNavigator nav = root.CreateNavigator();
XPathNodeIterator nodeIterator = nav.Select("/UserShortcuts/UserShortcut");
while (nodeIterator.MoveNext())
{
string sMenuLabel = nodeIterator.Current.SelectSingleNode
("MenuLabel").Value.Trim();
string sShortcutPath = nodeIterator.Current.SelectSingleNode
("ShortcutPath").Value.Trim();
if (System.Uri.IsWellFormedUriString
(sShortcutPath, UriKind.Absolute) == false)
{
FileInfo fiInfo = new FileInfo(sShortcutPath);
sShortcutPath = fiInfo.FullName;
}
if (m_UserShortcutItems.ContainsKey(sMenuLabel) == false)
{
m_UserShortcutItems.Add(sMenuLabel, sShortcutPath);
}
}
LoadUserShortcutsMenu
(mniUserShortcutsPlaceholder, mniUserShortcutsPlaceholder_Click);
}
catch (Exception ex)
{
throw new Exception("Error Loading User Shortcuts", ex);
}
finally
{
Directory.SetCurrentDirectory(sOldWorkingDirectory);
}
}
/// <summary>
/// Load the menu items into the placeholder's location and
/// then delete the placeholder item
/// </summary>
/// <param name="mniUserShortcutsPlaceholder"></param>
private static void LoadUserShortcutsMenu
(ToolStripMenuItem mniUserShortcutsPlaceholder,
EventHandler mniUserShortcutsPlaceholder_Click)
{
if (mniUserShortcutsPlaceholder == null)
return;
int iplaceholderloc = mniUserShortcutsPlaceholder.Owner.Items.IndexOf
(mniUserShortcutsPlaceholder);
IDictionaryEnumerator iusershortcuts =
UserShortcuts.UserShortcutItems.GetEnumerator();
while (iusershortcuts.MoveNext())
{
string sMenuName = Convert.ToString(iusershortcuts.Key);
ToolStripMenuItem mni = new ToolStripMenuItem(sMenuName);
mni.Click += new EventHandler(mniUserShortcutsPlaceholder_Click);
mniUserShortcutsPlaceholder.Owner.Items.Insert(iplaceholderloc, mni);
iplaceholderloc += 1;
}
mniUserShortcutsPlaceholder.Owner.Items.Remove(mniUserShortcutsPlaceholder);
}
/// <summary>
/// Launches the specified value/path/URL of the specified key (menu item)
/// </summary>
/// <param name="sKey"></param>
public static void LaunchUserShortcutItem(string sKey)
{
string sShortcutPath = Convert.ToString(UserShortcuts.UserShortcutItems[sKey]);
if ((System.Uri.IsWellFormedUriString(sShortcutPath, UriKind.Absolute) == true)
|| (File.Exists(sShortcutPath) == true))
{
ProcessStartInfo psi = new ProcessStartInfo(sShortcutPath);
psi.UseShellExecute = true;
Process.Start(psi);
}
}
}
结论
希望您觉得这篇文章和类有用 - 它已经在几个应用程序中派上用场了!
历史
- 2006 年 11 月 22 日:文章发布