C#實現(xiàn)創(chuàng)建桌面快捷方式與添加網(wǎng)頁到收藏夾的示例
今天來介紹一個小功能,就是把正在瀏覽的某網(wǎng)頁添加到收藏夾中。完成這個功能主要是兩步,首先要取得系統(tǒng)用戶的收藏夾目錄,第二是要根據(jù)獲得頁面地址在收藏夾目錄創(chuàng)建一個快捷方式。具體我們就一起來了解一下吧。
一、C#創(chuàng)建快捷方式
要創(chuàng)建快捷方式須引用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("創(chuàng)佳快捷方式成功!"); } catch (Exception ex) { MessageBox.Show("創(chuàng)佳快捷方式失??!"); } }
以上是C#里面調(diào)用相應(yīng)的方法創(chuàng)建快捷方式的方法;接下來要講的是C#里面將一個網(wǎng)頁添加到收藏夾里面,其實將網(wǎng)頁添加到收藏夾里的實質(zhì)是將給定的網(wǎng)頁生成一個快捷方式并放在收藏夾對應(yīng)的電腦的物理文件夾里面即可。
二、將網(wǎng)頁添加到收藏夾
首先,像第一步一樣引用相應(yīng)的dll
/// <summary> /// 添加收藏夾 /// </summary> /// <param name="url">對應(yīng)的網(wǎng)頁的url</param> /// <param name="saveName">保存的名稱</param> /// <param name="folderName">文件夾名稱</param> protected void AddToFavorites(String url, String saveName, String folderName) { System.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); } }
希望本文所述對你有所幫助,C#實現(xiàn)創(chuàng)建快捷方式與添加網(wǎng)頁到收藏夾的示例內(nèi)容就給大家介紹到這里了。希望大家繼續(xù)關(guān)注我們的網(wǎng)站!想要學(xué)習(xí)c#可以繼續(xù)關(guān)注本站。
相關(guān)文章
C#接口INotifyPropertyChanged使用方法
這篇文章介紹了C#接口INotifyPropertyChanged的使用方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-01-01解決C#中WebBrowser的DocumentCompleted事件不執(zhí)行的實現(xiàn)方法
本篇文章是對C#中WebBrowser的DocumentCompleted事件不執(zhí)行解決方法進行了詳細的分析介紹,需要的朋友參考下2013-05-05c# 怎樣簡潔高效的實現(xiàn)多個 Enum 自由轉(zhuǎn)換
這篇文章主要介紹了c# 怎樣簡潔高效的實現(xiàn)多個 Enum 自由轉(zhuǎn)換,幫助大家更好的進行c# 開發(fā),感興趣的朋友可以了解下2020-10-10