
開始菜單》程序菜單項:

添加到收藏夾:

URL:http://www.bianceng.cn/Programming/csharp/201410/45770.htm
相關函數代碼:
public const int SW_SHOWNORMAL = 1;
/// <summary>
/// 創建快捷方式。
/// </summary>
/// <param name="shortcutPath">快捷方式路徑。</param>
/// <param name="targetPath">目標路徑。</param>
/// <param name="workingDirectory">工作路徑。</param>
/// <param name="description">快捷鍵描述。</param>
public static bool CreateShortcut(string shortcutPath, string targetPath, string workingDirectory, string description, string iconLocation = null)
{
try
{
CShellLink cShellLink = new CShellLink();
IShellLink iShellLink = (IShellLink)cShellLink;
iShellLink.SetDescription(description);
iShellLink.SetShowCmd(SW_SHOWNORMAL);
iShellLink.SetPath(targetPath);
iShellLink.SetWorkingDirectory(workingDirectory);
if (!string.IsNullOrEmpty(iconLocation))
{
iShellLink.SetIconLocation(iconLocation, 0);
}
IPersistFile iPersistFile = (IPersistFile)iShellLink;
iPersistFile.Save(shortcutPath, false);
Marshal.ReleaseComObject(iPersistFile);
iPersistFile = null;
Marshal.ReleaseComObject(iShellLink);
iShellLink = null;
Marshal.ReleaseComObject(cShellLink);
cShellLink = null;
return true;
}
catch //(System.Exception ex)
{
return false;
}
}
/// <summary>
/// 創建桌面快捷方式
/// </summary>
/// <param name="targetPath">可執行文件路徑</param>
/// <param name="description">快捷方式名稱</param>
/// <param name="iconLocation">快捷方式圖標路徑</param>
/// <param name="workingDirectory">工作路徑</param>
/// <returns></returns>
public static bool CreateDesktopShortcut(string targetPath, string description, string iconLocation = null, string workingDirectory = null)
{
if (string.IsNullOrEmpty(workingDirectory))
{
workingDirectory = Shortcut.GetDeskDir();
}
return Shortcut.CreateShortcut(Shortcut.GetDeskDir() + "\\" + description + ".lnk", targetPath, workingDirectory, description, iconLocation);
}
/// <summary>
/// 創建程序菜單快捷方式
/// </summary>
/// <param name="targetPath">可執行文件路徑</param>
/// URL:http://www.bianceng.cn/Programming/csharp/201410/45770.htm
/// <param name="description">快捷方式名稱</param>
/// <param name="menuName">程序菜單中子菜單名稱,為空則不創建子菜單</param>
/// <param name="iconLocation">快捷方式圖標路徑</param>
/// <param name="workingDirectory">工作路徑</param>
/// <returns></returns>
public static bool CreateProgramsShortcut(string targetPath, string description, string menuName, string iconLocation = null, string workingDirectory = null)
{
if (string.IsNullOrEmpty(workingDirectory))
{
workingDirectory = Shortcut.GetProgramsDir();
}
string shortcutPath = Shortcut.GetProgramsDir();
if (!string.IsNullOrEmpty(menuName))
{
shortcutPath += "\\" + menuName;
if (!System.IO.Directory.Exists(shortcutPath))
{
try
{
System.IO.Directory.CreateDirectory(shortcutPath);
}
catch //(System.Exception ex)
{
return false;
}
}
}
shortcutPath += "\\" + description + ".lnk";
return Shortcut.CreateShortcut(shortcutPath, targetPath, workingDirectory, description, iconLocation);
}
/// <summary>
/// 將網頁添加到收藏夾
/// </summary>
/// <param name="url">要添加到收藏夾的網址</param>
/// <param name="description">標題</param>
/// <param name="folderName">收藏文件夾名稱</param>
/// <param name="iconLocation">圖標文件路徑</param>
/// <param name="workingDirectory">工作路徑</param>
/// <returns></returns>
public static bool AddFavorites(string url, string description, string folderName, string iconLocation = null, string workingDirectory = null)
{
if (string.IsNullOrEmpty(workingDirectory))
{
workingDirectory = Shortcut.GetProgramsDir();
}
string shortcutPath = Shortcut.GetFavoriteDir();
if (!string.IsNullOrEmpty(folderName))
{
shortcutPath += "\\" + folderName;
if (!System.IO.Directory.Exists(shortcutPath))
{
try
{
System.IO.Directory.CreateDirectory(shortcutPath);
}
catch //(System.Exception ex)
{
return false;
}
}
}
shortcutPath += "\\" + description + ".lnk";
return Shortcut.CreateShortcut(shortcutPath, url, workingDirectory, description, iconLocation);
}
本文章源代碼Src下載地址:http://download.csdn.net/detail/testcs_dn/5141580