您的位置:首页 > 编程语言 > C#

C#创建快捷方式与添加网页到收藏夹

2012-08-07 14:43 253 查看
一、C#创建快捷方式

要创建快捷方式须引用IWshRuntimeLibrary.dll,引用方式为:对项目添加引用——>选择COM组件——>选择"Windows Script Host Object Model"确定,则添加成功!接下来就是编码:

[c-sharp] view plaincopyprint?

/// <summary>

/// 生成快捷方式

/// </summary>

/// <param name="targetPath">原目标位置</param>

/// /// <param name="savePath">保存快捷方式的位置</param>

protected void CreateShortcuts(String targetPath, String savePath,String saveName)

{

IWshRuntimeLibrary.IWshShell shell_class = new IWshRuntimeLibrary.IWshShell_ClassClass();

IWshRuntimeLibrary.IWshShortcut shortcut = null;

if (!Directory.Exists(targetPath))

return;

if (!Directory(savePath))

Directory.CreateDirectory(savePath);

try

{

shortcut = shell_class.CreateShortcut(savePath + @"/" + saveName + ".lnk") as IWshRuntimeLibrary.IWshShortcut;

shortcut.TargetPath = targetPath;

shortcut.Save();

MessageBox.Show("创佳快捷方式成功!");

}

catch (Exception ex)

{

MessageBox.Show("创佳快捷方式失败!");

}

}

[c-sharp] view plaincopyprint?

/// <summary>

/// 添加收藏夹

/// </summary>

/// <param name="url">对应的网页的url</param>

/// <param name="saveName">保存的名称</param>

/// <param name="folderName">文件夹名称</param>

protected void AddToFavorites(String url, String saveName, String folderName)

{

System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new Uri(url));

request.Method = "GET";

request.Timeout = 10000;

try

{

System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();

if (response.StatusCode == System.Net.HttpStatusCode.OK)

{

//获取当前用户的收藏夹的物理文件夹位置

String favoritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);

String savePath = favoritesPath;

if (!String.IsNullOrEmpty(folderName))

{

savePath += @"/" + folderName;

if (!Directory.Exists(savePath))

Directory.CreateDirectory(savePath);

}

IWshRuntimeLibrary.WshShell shell_class = new IWshRuntimeLibrary.WshShellClass();

IWshRuntimeLibrary.IWshShortcut shortcut = null;

try

{

shortcut = shell_class.CreateShortcut(favoritesPath + @"/" + saveName + ".lnk") as IWshRuntimeLibrary.IWshShortcut;

shortcut.TargetPath = url;

shortcut.Save();

MessageBox.Show("添加成功");

}

catch (Exception ex)

{

MessageBox.Show("添加失败");

}

}

else

{

MessageBox.Show("请求失败");

}

}

catch (Exception ex)

{

MessageBox.Show(ex.Message);

}

}

/// <summary>
/// 添加收藏夹
/// </summary>
/// <param name="url">对应的网页的url</param>
/// <param name="saveName">保存的名称</param>
/// <param name="folderName">文件夹名称</param>
protected void AddToFavorites(String url, String saveName, String folderName)
{
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new Uri(url));
request.Method = "GET";
request.Timeout = 10000;
try
{
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
//获取当前用户的收藏夹的物理文件夹位置
String favoritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
String savePath = favoritesPath;
if (!String.IsNullOrEmpty(folderName))
{
savePath += @"/" + folderName;
if (!Directory.Exists(savePath))
Directory.CreateDirectory(savePath);
}
IWshRuntimeLibrary.WshShell shell_class = new IWshRuntimeLibrary.WshShellClass();
IWshRuntimeLibrary.IWshShortcut shortcut = null;
try
{
shortcut = shell_class.CreateShortcut(favoritesPath + @"/" + saveName + ".lnk") as IWshRuntimeLibrary.IWshShortcut;
shortcut.TargetPath = url;
shortcut.Save();
MessageBox.Show("添加成功");
}
catch (Exception ex)
{
MessageBox.Show("添加失败");
}
}
else
{
MessageBox.Show("请求失败");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}


说明:以上内容参考于:http://dev.firnow.com/course/3_program/cshapo/csharpjs/20100727/495094.htmlhttp://blog.csdn.net/rijing2000/archive/2005/03/21/325589.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: