frmOpenFavorites.cs
上传用户:hxjiliang
上传日期:2022-04-23
资源大小:182k
文件大小:2k
源码类别:

浏览器

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using IWshRuntimeLibrary;
  9. namespace MyWebIE
  10. {
  11.     public partial class frmOpenFavorites : Form
  12.     {
  13.         public frmOpenFavorites()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         public string IUrl;
  18.         public string ITitle;
  19.         private void button1_Click(object sender, EventArgs e)
  20.         {
  21.             try
  22.             {
  23.                 addFavorites(IUrl, ITitle, "Favorites");
  24.             }
  25.             catch (Exception ex)
  26.             {
  27.                 MessageBox.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  28.             }
  29.         }
  30.         /// <summary>
  31.         /// 首先要添加应用,选择COM组件中的“Windows Script Host Object Model”
  32.         /// </summary>
  33.         /// <param name="url"></param>
  34.         /// <param name="filename"></param>
  35.         /// <param name="savepath"></param>
  36.         private void addFavorites(string url, string filename, string savepath)
  37.         {
  38.             string path = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
  39.             if (!System.IO.File.Exists(path + "\" + filename + savepath + ".url"))
  40.             {
  41.                 IWshShell_Class shell = new IWshShell_ClassClass();
  42.                 IWshURLShortcut shortcut = null;
  43.                 if (savepath == "Favorites")
  44.                 {
  45.                     shortcut = shell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Favorites) + "\" + filename + ".url") as IWshURLShortcut;
  46.                     if (MessageBox.Show("网址已经添加到收藏夹", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
  47.                     {
  48.                         this.Close();
  49.                     }
  50.                 }
  51.                 else
  52.                 {
  53.                     shortcut = shell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Favorites) + "\" + savepath + "\" + filename + ".url") as IWshURLShortcut;
  54.                 }
  55.                 shortcut.TargetPath = url;
  56.                 shortcut.Save();
  57.             }
  58.         }
  59.         private void frmOpenFavorites_Load(object sender, EventArgs e)
  60.         {
  61.             textBox1.Text = ITitle;
  62.         }
  63.         private void button2_Click(object sender, EventArgs e)
  64.         {
  65.             this.Close();
  66.         }
  67.     }
  68. }