c#通过纯代码创建桌面快捷方式、创建程序菜单项、将网页添加到收藏夹
开始菜单》程序菜单项:
添加到收藏夹:
相关函数代码:
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>
/// <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);
}
以上就是c#通过纯代码创建桌面快捷方式,程序菜单项、将网页添加到收藏夹的详解(图)的详细内容。