C#如何創(chuàng)建快捷方式和添加網(wǎng)頁到收藏夾

    時間:2024-09-19 07:04:17 ACCP培訓(xùn) 我要投稿
    • 相關(guān)推薦

    C#如何創(chuàng)建快捷方式和添加網(wǎng)頁到收藏夾

      為了方便大家的學(xué)習(xí),下面是小編整理的關(guān)于C#如何創(chuàng)建快捷方式和添加網(wǎng)頁到收藏夾的方法,歡迎參考!

      一、C#創(chuàng)建快捷方式

      要創(chuàng)建快捷方式須引用IWshRuntimeLibrary.dll,引用方式為:對項目添加引用——>選擇COM組件——>選擇"Windows Script Host Object Model"確定,則添加成功!接下來就是編碼:

      ///

      /// 生成快捷方式

      ///

      /// 原目標位置

      /// /// 保存快捷方式的位置

      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

      ///

      /// 添加收藏夾

      ///

      /// 對應(yīng)的網(wǎng)頁的url

      /// 保存的名稱

      /// 文件夾名稱

      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);

      }

      }

    【C#如何創(chuàng)建快捷方式和添加網(wǎng)頁到收藏夾】相關(guān)文章:

    如何識別和防御Web網(wǎng)頁木馬09-11

    word如何添加標題樣式06-26

    PPT如何添加超鏈接09-28

    如何添加用戶對網(wǎng)站的黏度?08-08

    c#和javascript函數(shù)相互調(diào)用示例10-16

    如何在模板之間添加素材07-16

    ppt中如何添加超鏈接09-05

    C# AutoResetEvent的使用10-29

    會聲會影如何添加遮罩07-08

    linux如何給文件或目錄添加apache權(quán)限11-03

    91久久大香伊蕉在人线_国产综合色产在线观看_欧美亚洲人成网站在线观看_亚洲第一无码精品立川理惠

      亚洲视频在线精品 | 亚洲欧美国产97综合首页 | 性网址在线观看 | 三级亚洲中文字幕 | 亚洲天堂午夜影院中文字幕 | 在线视频免费国产午夜 |