C#完成創立桌面快捷方法與添加網頁到珍藏夾的示例。本站提示廣大學習愛好者:(C#完成創立桌面快捷方法與添加網頁到珍藏夾的示例)文章只能為提供參考,不一定能成為您想要的結果。以下是C#完成創立桌面快捷方法與添加網頁到珍藏夾的示例正文
明天來引見一個小功效,就是把正在閱讀的某網頁添加到珍藏夾中。完成這個功效重要是兩步,起首要獲得體系用戶的珍藏夾目次,第二是要依據取得頁面地址在珍藏夾目次創立一個快捷方法。詳細我們就一路來懂得一下吧。
1、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("創佳快捷方法掉敗!");
}
}
以上是C#外面挪用響應的辦法創立快捷方法的辦法;接上去要講的是C#外面將一個網頁添加到珍藏夾外面,其實將網頁添加到珍藏夾裡的本質是將給定的網頁生成一個快捷方法並放在珍藏夾對應的電腦的物理文件夾外面便可。
2、將網頁添加到珍藏夾
起首,像第一步一樣援用響應的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<a href="http://lib.csdn.net/base/dotnet" class='replace_word' title=".NET常識庫" target='_blank' style='color:#df3434; font-weight:bold;'>.NET</a>.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#完成創立快捷方法與添加網頁到珍藏夾的示例內容就給年夜家引見到這裡了。願望年夜家持續存眷我們的網站!想要進修c#可以持續存眷本站。