一、C#创建快捷方式
要创建快捷方式须引用IWshRuntimeLibrary.dll,引用方式为:对项目添加引用——>选择COM组件——>选择"Windows Script Host Object Model"确定,则添加成功!接下来就是编码:
- /// <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("创佳快捷方式失败!");
- }
- }
/// <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#里面调用相应的方法创建快捷方式的方法;接下来要讲的是C#里面将一个网页添加到收藏夹里面,其实将网页添加到收藏夹里的实质是将给定的网页生成一个快捷方式并放在收藏夹对应的电脑的物理文件夹里面即可。
二、将网页添加到收藏夹
首先,像第一步一样引用相应的dll
- /// <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); } }
说明:以上内容参考于:和